Spring Boot Logging
Below is shown the code for a simple and fast way for log messages in a Spring Boot web application.
Dependencies
You don’t need any additional dependencies, Spring Boot already have all what is need. Only make sure you have at least the version 1.2.0 of Spring Boot.
Log object
Add the org.slf4j.Logger object on each java class where you want to log something:
package netgloo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// ...
public class SomeClass {
// Define the logger object for this class
private final Logger log = LoggerFactory.getLogger(this.getClass());
// The rest of the class
// ...
}
Inside class’s methods you can now use the log
object for write messages at some log level:
public void someMethod() {
// ...
// Log a simple message
log.debug("debug level log");
log.info("info level log");
log.error("error level log");
// ...
}
You will see your log messages in the Spring Boot’s console output, like this:
2016-05-22 12:00:06.589 DEBUG 9872 --- [nio-8080-exec-1] netgloo.controllers.HomeController : debug level log 2016-05-22 12:00:06.590 INFO 9872 --- [nio-8080-exec-1] netgloo.controllers.HomeController : info level log 2016-05-22 12:00:06.593 ERROR 9872 --- [nio-8080-exec-1] netgloo.controllers.HomeController : error level log
You can also put your log in a file setting the logging.file
configuration in the application.properties
file (see below the Further configurations section).
Application.properties
Finally, in the application.properties
file you can define the log level for your loggers:
src/main/resources/application.properties
# Level for loggers on classes inside the root package "netgloo" (and its
# sub-packages)
# Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
logging.level.netgloo = DEBUG
If you define the INFO
level for the package netgloo
then all logs printed using the info method up to the fatal will be printed out, while debug ones will be ignored.
That’s all.
Further configurations
Logging formatted string
It is possible to log formatted string using the following format:
log.info("another info log with {}, {} and {} arguments", 1, "2", 3.0);
Additional configurations in the application.properties
In the application.properties file you can put some additional configurations:
# Fine-tuning a specific logger (for a single class)
logging.level.netgloo.controllers.HomeController = TRACE
# Specify the level for spring boot and hibernate's loggers
logging.level.org.springframework.web = DEBUG
logging.level.org.hibernate = ERROR
# Log file location (in addition to the console)
logging.file = /var/netgloo_blog/logs/application.log
Logback
As said in the official reference, Spring Boot use Logback by default as logging service. You can set advanced configurations using the logback.xml
file, placing it in src/main/resources/logback.xml (so Maven or Gradle will copy it in the classpath root). See here and here for two simple examples about the logback configuration file.
Try the code yourself
Try yourself getting the code for this post from our github repository:
https://github.com/netgloo/spring-boot-samples/tree/master/spring-boot-logging
References
http://docs.spring.io/spring-boot/docs/1.2.0.RELEASE/reference/html/howto-logging.html
https://wiki.base22.com/display/btg/How+to+setup+SLF4J+and+LOGBack+in+a+web+app+-+fast
http://www.javacodegeeks.com/2011/12/configure-logback-logging-with-spring.html
https://stackoverflow.com/questions/8262310/any-reason-to-use-private-instead-of-private-final-static-on-the-logback-logger
-
Odilon Alves de Oliveira
-
Faraz
-