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.
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
Notice that the Spring Boot starter already includes dependencies for logging...
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.
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.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...
#output to a temp_folder/file
#output to a file
#output to a file called spring.log in the specified path
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.