Skip to end of metadata
Go to start of metadata

Logging with Spring Boot is dead simple. Everything's pretty much setup and ready to go. In this post, I provide some quick and simple tips to get your Spring Boot logs rolling.

About Logging Dependencies

If you use the ‘Starters’, Logback will be used with appropriate routing included to ensure that dependent libraries that use Java Util Logging, Commons Logging, Log4J or SLF4J will all work correctly. Let's suppose, for example, that you're using the web starter in your Maven pom.xml file, as shown below.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Generally you won’t need to change any logging dependencies and the Spring Boot defaults will work just fine. That is to say, you don't need to add any additional dependencies for logging to the pom. You can verify this by printing a tree representation of your project dependencies. On the command line, change to your project directory and executing the following command.

Print Maven Dependency Tree
mvn dependency:tree

Notice that the Spring Boot starter already includes dependencies for logging...

[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.5.2.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:1.5.2.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.5.2.RELEASE:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.1.11:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.1.11:compile
[INFO] |  |  |  +- org.slf4j:jcl-over-slf4j:jar:1.7.24:compile
[INFO] |  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.24:compile
[INFO] |  |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.24:compile

Configure Log Levels

The easiest way to configure logging levels is in the application.properties file. If you don't already have one, create an application.properties file in the root of the resources folder. Then, simply prefix Java packages and classes with logging.level as shown below. Notice that you can configure the root logger at a specific level first, then get more specific with other loggers.

application.properties
logging.level.root=INFO
logging.level.com.cburleson.rdfx=TRACE

Put Logging Code in Your Classes

Now, we can use SLF4J for logging. Here's how.

Add the following to the imports section of your java code:


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Add the following at the top of your class in the global section (just under the line that declares your class public class Whatever extends Whatever). Change the name of the class (MyClassName) in the getLogger method call, of course. Name it the same as the class you're dropping this code into.


static final Logger LOG = LoggerFactory.getLogger(MyClassName.class);

To test quickly, you can throw some logging statements in your code somewhere where you know they'll be fired right away when you run your app. For example:


LOG.trace("Hello World!");
LOG.debug("How are you today?");
LOG.info("I am fine.");
LOG.warn("I love programming.");
LOG.error("I am programming.");

The default log configuration will echo messages to the console as they are written. If your terminal supports ANSI, color output will be used to aid readability.

Log to a File

If you want to write log files in addition to the console, you can set a logging.file or logging.path property in your application.properties. For example...

logging.level.root=INFO
logging.level.com.cburleson.rdfx=TRACE


#output to a temp_folder/file
logging.file=${java.io.tmpdir}/myapp.log

#output to a file
#logging.file=/Users/cburleson/myapp.log


#output to a file called spring.log in the specified path
#logging.path=/var/log

Conclusion

As you can see, logging from your Spring Boot application is piece of cake. Of course, there's a lot more that you can do as your requirements dictate. For more information, take a look at Logging, in the Spring Boot Reference Guide.

References

  • Logging, in the Spring Boot Reference Guide