Spring Boot has no mandatory logging dependence, except for the commons-logging
API, of
which there are many implementations to choose from. To use Logback
you need to include it, and some bindings for commons-logging
on the classpath. The
simplest way to do that is through the starter poms which all depend on
spring-boot-starter-logging
. For a web application you only need
spring-boot-starter-web
since it depends transitively on the logging starter.
For example, using Maven:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
Spring Boot has a LoggingSystem
abstraction that attempts to configure logging based on
the content of the classpath. If Logback is available it is the first choice.
If the only change you need to make to logging is to set the levels of various loggers
then you can do that in application.properties
using the "logging.level" prefix, e.g.
logging.level.org.springframework.web=DEBUG logging.level.org.hibernate=ERROR
You can also set the location of a file to log to (in addition to the console) using "logging.file".
To configure the more fine-grained settings of a logging system you need to use the native
configuration format supported by the LoggingSystem
in question. By default Spring Boot
picks up the native configuration from its default location for the system (e.g.
classpath:logback.xml
for Logback), but you can set the location of the config file
using the "logging.config" property.
If you put a logback.xml
in the root of your classpath it will be picked up from
there
(or logback-spring.xml
to take advantage of the templating features provided by Boot). Spring Boot provides a default base configuration that you can include if you just
want to set levels, e.g.
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <logger name="org.springframework.web" level="DEBUG"/> </configuration>
If you look at that base.xml
in the spring-boot jar, you will see that it uses
some useful System properties which the LoggingSystem
takes care of creating for you.
These are:
${PID}
the current process ID.${LOG_FILE}
if logging.file
was set in Boot’s external configuration.${LOG_PATH}
if logging.path
was set (representing a directory for
log files to live in).${LOG_EXCEPTION_CONVERSION_WORD}
if logging.exception-conversion-word
was set in
Boot’s external configuration.Spring Boot also provides some nice ANSI colour terminal output on a console (but not in
a log file) using a custom Logback converter. See the default base.xml
configuration
for details.
If Groovy is on the classpath you should be able to configure Logback with
logback.groovy
as well (it will be given preference if present).
Spring Boot also supports either Log4j or
Log4j 2 for logging configuration, but only if one
of them is on the classpath. If you are using the starter poms for assembling
dependencies that means you have to exclude Logback and then include your chosen version
of Log4j instead. If you aren’t using the starter poms then you need to provide
commons-logging
(at least) in addition to your chosen version of Log4j.
The simplest path is probably through the starter poms, even though it requires some jiggling with excludes, .e.g. in Maven:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> </dependency>
To use Log4j 2, simply depend on spring-boot-starter-log4j2
rather than
spring-boot-starter-log4j
.
Note | |
---|---|
The use of one of the Log4j starters gathers together the dependencies for
common logging requirements (e.g. including having Tomcat use |
In addition to its default XML configuration format, Log4j 2 also supports YAML and JSON
configuration files. To configure Log4j 2 to use an alternative configuration file format
all you need to do is add an appropriate dependency to the classpath. To use YAML, add a
dependency on com.fasterxml.jackson.dataformat:jackson-dataformat-yaml
and Log4j 2 will
look for configuration files names log4j2.yaml
or log4j2.yml
. To use JSON, add a
dependency on com.fasterxml.jackson.core:jackson-databind
and Log4j 2 will look for
configuration files named log4j2.json
or log4j2.jsn