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 to the POM for logging. 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.
Notice that the Spring Boot starter already includes dependencies for logging...
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.
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:
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.
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:
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...
Using SLF4J over Log4j
Now, if you want to use SLF4 over Log4j 2, and a log4j configuration file, the setup is a little different. In your Maven, POM, you must exclude
spring-boot-starter-logging and then add a dependency for
spring-boot-starter-log4j2 as shown below.
Then you need to have a log4j2.xml file on the classpath; for example - in
src/main/resources. Here's a simple log4j2.xml file example.
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.
- Logging, in the Spring Boot Reference Guide
Blog:Logging with Spring Boot (Blog)