Spring Boot manages internal logging through commons logging. A Spring Boot application can use any logging framework that implements Apache commons logging. The default configurations supported by the Spring Boot framework include Java Util Logging, Log4J2, and Logback. By default, logs are shown in the console window. However, through configuration in application.properties, logs can be directed to a specific file. The logger levels available in Spring Boot applications are trace, debug, info, warn, error, and fatal.
In this post, we’ll explore different methods for implementing logging in a Spring Boot application and how to effectively log data. We’ll delve into the supported loggers in Spring Boot and discuss techniques for writing logs to files, printing them, and disabling console logs within the Spring Boot framework.
1. Default Logging in Spring Boot
Spring Boot comes equipped with a default logger that requires zero configuration for activation. The logger is automatically enabled upon the creation of a Spring Boot project, and logs are initially printed to the console window. Apache Common Logging is a prerequisite for Spring Boot, and it is enabled by default, ensuring that all logs are displayed in the console window when the application starts.
If a default Spring Boot project is created and executed, the console window will display logs similar to those depicted in the following image.
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.7.3) 2023-06-02 11:25:20.183 INFO 10700 --- [ main] com.test.FirstProgramApplication : Starting FirstProgramApplication using Java 17.0.4 on DESKTOP-DMI8H0A with PID 10700 (E:\STS\workspace\FirstProgram\target\classes started by Admin in E:\STS\workspace\FirstProgram) 2023-06-02 11:25:20.186 INFO 10700 --- [ main] com.test.FirstProgramApplication : No active profile set, falling back to 1 default profile: "default" 2023-06-02 11:25:20.828 INFO 10700 --- [ main] com.test.FirstProgramApplication : Started FirstProgramApplication in 1.142 seconds (JVM running for 2.114)
2. How to log data in Spring Boot
To log data in a Java class, a Logger class object should be created at the class level. This object can then be utilized to log data using logger level APIs, including trace, debug, info, warn, error, and fatal. The logger levels determine the priority of the logs, and based on the logger configuration, they will be printed in log files or the console window.
In the example below, the main class of the Spring Boot application invokes all logger level APIs. By default, the logger level is set to info in the Spring Boot application, and logs with a criticality of info or higher will be displayed in the console window.
package com.test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class FirstProgramApplication { private final static Logger logger = LoggerFactory.getLogger(FirstProgramApplication.class); public static void main(String[] args) { SpringApplication.run(FirstProgramApplication.class, args); logger.trace("trace"); logger.debug("debug"); logger.info("info"); logger.warn("warning"); logger.error("error"); } }
Output
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.7.3) 2023-06-02 11:25:20.183 INFO 10700 --- [ main] com.test.FirstProgramApplication : Starting FirstProgramApplication using Java 17.0.4 on DESKTOP-DMI8H0A with PID 10700 (E:\STS\workspace\FirstProgram\target\classes started by Admin in E:\STS\workspace\FirstProgram) 2023-06-02 11:25:20.186 INFO 10700 --- [ main] com.test.FirstProgramApplication : No active profile set, falling back to 1 default profile: "default" 2023-06-02 11:25:20.828 INFO 10700 --- [ main] com.test.FirstProgramApplication : Started FirstProgramApplication in 1.142 seconds (JVM running for 2.114) 2023-06-02 11:25:20.835 INFO 10700 --- [ main] com.test.FirstProgramApplication : info 2023-06-02 11:25:20.835 WARN 10700 --- [ main] com.test.FirstProgramApplication : warning 2023-06-02 11:25:20.835 ERROR 10700 --- [ main] com.test.FirstProgramApplication : error
3. How to change log level
The default log level for a Spring Boot application is INFO. However, this log level can be modified through various means, such as the application.properties file, the application.yaml file, command line arguments, or alternative logger configuration files like logback.xml.
In the following example, the log level is set to TRACE, which represents a more detailed subset of logs. Consequently, all logs, regardless of their level, will be printed.
application.properties
logging.level.com.test=TRACE
Command line
java -jar target/FirstProgram-0.0.1-SNAPSHOT.jar --trace
Output
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.7.3) 2023-06-02 11:25:21.117 INFO 2012 --- [ main] com.test.FirstProgramApplication : Starting FirstProgramApplication using Java 17.0.4 on DESKTOP-DMI8H0A with PID 2012 (E:\STS\workspace\FirstProgram\target\classes started by Admin in E:\STS\workspace\FirstProgram) 2023-06-02 11:25:21.120 DEBUG 2012 --- [ main] com.test.FirstProgramApplication : Running with Spring Boot v2.7.3, Spring v5.3.22 2023-06-02 11:25:21.121 INFO 2012 --- [ main] com.test.FirstProgramApplication : No active profile set, falling back to 1 default profile: "default" 2023-06-02 11:25:21.758 INFO 2012 --- [ main] com.test.FirstProgramApplication : Started FirstProgramApplication in 1.097 seconds (JVM running for 1.948) 2023-06-02 11:25:21.800 TRACE 2012 --- [ main] com.test.FirstProgramApplication : trace 2023-06-02 11:25:21.801 DEBUG 2012 --- [ main] com.test.FirstProgramApplication : debug 2023-06-02 11:25:21.801 INFO 2012 --- [ main] com.test.FirstProgramApplication : info 2023-06-02 11:25:21.801 WARN 2012 --- [ main] com.test.FirstProgramApplication : warning 2023-06-02 11:25:21.801 ERROR 2012 --- [ main] com.test.FirstProgramApplication : error
4. Write Logger to File
The Spring Boot application typically writes log statements to the console window by default. If the log buffer exceeds its limit, the console log will be rolled out. To ensure the preservation of all logs, they should be written to a log file. Log files offer accessibility and the ability to store a considerable number of logs. The Spring Boot application can be configured to log to a file using the application.properties
file.
application.properties
logging.file.name=firstprogram.log logging.file.path=/logs
5. Disable Console Log
When developing a large Spring Boot application, printing logs in the console window can pose a security risk, as anyone with access to the data can view it. To mitigate this risk, it’s advisable to disable the printing of logs in the console window. Spring Boot provides the flexibility to configure this behavior and allows you to turn off log printing to the console.
spring.main.banner-mode=off logging.pattern.console=
6. Logging using Logback Configuration
Spring Boot applications provide default log management through the application.properties
file. Additionally, Spring Boot allows for logger configuration using logback configurations. Logback configuration files offer customization options for logs and loggers. They assist in configuring different log levels for various package structures. Logback configuration files can be employed to set up logging strategies, including rolling policies, console or file output, file name formats, and more.
When placed in the src/main/resources
folder, logback configuration files are automatically detected by the Spring Boot application.
- logback-spring.xml
- logback.xml
- logback.properties
- logback-spring.groovy
- logback.groovy
logback.xml
<?xml version = "1.0" encoding = "UTF-8"?> <configuration> <appender name = "consoleoutput" class = "ch.qos.logback.core.ConsoleAppender"></appender> <appender name = "logfile" class = "ch.qos.logback.core.FileAppender"> <file>c:/application.log</file> </appender> <root level = "INFO"> <appender-ref ref = "logfile"/> <appender-ref ref = "consoleoutput"/> </root> </configuration>