Logging with Spring Boot

Spring boot comes with preconfigured logging using logback as default logging framework.
As I faced some issues configuring logging in a multi maven project I decided to create a small dummy project to fiddle with spring boot logging and some settings.

Here´s the spring boot documentation about logging.

The default logging with Spring boot

If you don´t configure any logging in our spring boot application it uses logback and spring boot manages logging. Logback implements the slf4j API, so we can import Logger and LoggerFactory form slf4j. Which is defined as dependency by the parent spring boot project.
You can add your own logback logger like this:

By default spring boot will log anything from info level and above (info, warn, …).

Logging configuration in application.properties

If we configure logging in spring boot´s application.properties we can set a debug=true property which will result in a much more verbose output (the default is debug=false) including an auto-configuration report for spring boot. Logmessages on debug level of our classes will still be supressed and only info level will be printed without further configuration. See next section to see how to define debug level on class or package level for our own classes.

Define loglevels in application.properties

If we need more fine grained logging configuration we have several options.
The easiest way would be to define more fine grained logging in application.properties as described in the spring boot documentation (26.4) where loglevels can be set on package and/or classlevel similar to log4j.properties. This is also required in combination with debug=true if we want own logging statements to be printed to console on debug level:

This will add our logging statements on debug lebel to the console output:

If we want to suppress logging on debug level for spring boot (and other) classes that will print on debug level if debug=true is set, it is important that we remove the debug=true setting. Setting to debug=false does not do the job (?!), it must be removed or commented (using #). That´s an easy way to configure logging on console. In this example it results in only one line printed to console:

Using logging dependent configuration

If we need even more fine grained logging configuration or if we want to define special appenders or settings specific to a certain logging framework we should define a separate logging configuration (26.5 in spring boot reference). Spring supports many logging configuration files, here´s an example for a logback configuration file that will be completely controlled by spring boot if named accordingly, for logback  logback-spring.xml (recommended by spring):

This allows more finegrained settings and every confguration setting that e.g. logback allows.
Be aware that eventually conflicting loglevel definitions in application.properties overwrite the settings in logback-spring.xml. You probably want to avoid configure logging in both files.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.