Introduction
Logging in spring boot is very flexible and easy to configure. Spring Boot’s default configurations provides a support for the use of Java Util Logging, Log4j2, and Logback. In each case, loggers are pre-configured to use console output with optional file output also available.
A new, freshly-generated Spring Boot application doesn’t come with any explicit logging configuration, and it will use Logback by default, the logging framework for Java, and the successor to the old log4j.
Logging is configured automatically when you add the spring-boot-starter-web
dependency to your project.
Spring boot’s internal logging is written with Apache Commons Logging so it is one and only mandatory dependency. Till, boot 1.x – we had to import it manually. Since boot 2.x, it is downloaded transitively. To be more precise, spring-boot-starter-web
depends on spring-boot-starter-logging
, which pulls in spring-jcl
for us.
File Output
By default, Spring Boot logs only to the console and does not write log files. To make Spring Boot write to a log file, you can set the logging.path property in your application.properties
file:
logging.path = ./ # spring.log in current folder logging.path = relativepath/logs # relativepath/logs/spring.log logging.path = /fullpath/logs # /fullpath/logs/spring.log
With this configuration, Spring Boot will write to the console and also to a log file called spring.log
, at the path you specify.
If you want to choose your own log filename instead of spring.log
, then you can set the property logging.file, in your application.properties
, e.g.:
logging.file = logfile.log # in current folder logging.file = relativepath/logfile.log # relative path with filename logging.file = /fullpath/logfile.log # full path with filename
Log files rotate when they reach 10 MB and, as with console output, ERROR
-level, WARN
-level, and INFO
-level messages are logged by default. Size limits can be changed using the logging.file.max-size
property. Previously rotated files are archived indefinitely unless the logging.file.max-history
property has been set.
Logback does not have a
FATAL
level. It is mapped toERROR
.
We can configure separate property files for each environment like application-dev.properties
and application-prod.properties
. Follow the naming convention of application-{environment}.properties
where {environment}
is replaced with the environment name. Depending on your VM options or environment variables, one of these can be chosen.
Spring Cloud Config doesn’t support default Logback properties.
Log Format
The default log output from Spring Boot resembles the following example:
2020-03-08 11:57:51.102 INFO 55469 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52 2020-03-08 11:57:51.203 INFO 55469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-03-08 11:57:51.203 INFO 55469 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1358 ms 2020-03-08 11:57:51.608 INFO 55469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
The following items are output:
- Date and Time: Millisecond precision and easily sort-able.
- Log Level:
ERROR
,WARN
,INFO
,DEBUG
, orTRACE
. - Process ID.
- A
---
separator to distinguish the start of actual log messages. - Thread name: Enclosed in square brackets (may be truncated for console output).
- Logger name: This is usually the source class name (often abbreviated).
- The log message.
Customization
To help with the customization, some other properties are transferred from the Spring Environment
to System properties, as described in the following table:
Spring Environment | System Property | Comments |
---|---|---|
logging.exception-conversion-word | LOG_EXCEPTION_CONVERSION_WORD | The conversion word used when logging exceptions. |
logging.file | LOG_FILE | If defined, it is used in the default log configuration. |
logging.file.max-size | LOG_FILE_MAX_SIZE | Maximum log file size (if LOG_FILE enabled). (Only supported with the default Logback setup.) |
logging.file.max-history | LOG_FILE_MAX_HISTORY | Maximum number of archive log files to keep (if LOG_FILE enabled). (Only supported with the default Logback setup.) |
logging.path | LOG_PATH | If defined, it is used in the default log configuration. |
logging.pattern.console | CONSOLE_LOG_PATTERN | The log pattern to use on the console (stdout). (Only supported with the default Logback setup.) |
logging.pattern.dateformat | LOG_DATEFORMAT_PATTERN | Appender pattern for log date format. (Only supported with the default Logback setup.) |
logging.pattern.file | FILE_LOG_PATTERN | The log pattern to use in a file (if LOG_FILE is enabled). (Only supported with the default Logback setup.) |
logging.pattern.level | LOG_LEVEL_PATTERN | The format to use when rendering the log level (default %5p ). (Only supported with the default Logback setup.) |
You can customize the default logging configuration by:
- Setting some specific Spring Boot properties (these can go in your
application.properties
,application.yml
or as environment variables) - Adding a
logback.xml
onto the classpath, which Spring Boot will detect and use to configure Logback - Adding a
logback-spring.xml
onto the classpath, which Spring Boot will detect and use to configure Logback.
Thanks for this article. There’s a simple way to load the logging configuration from a logback.xml placed outside the classpath?
If your Logback.xml file is outside the classpath, you need to point to its location using the Logback.configurationFile system property, like this.
-DLogback.configurationFile=/path/to/Logback.xml