Spring Boot GoT: Game of Trace!
Start with the basics of Spring Boot logs, and learn how to configure logging, test log severity, and adjust log levels using the LoggingSystem class.
Join the DZone community and get the full member experience.
Join For FreeWhen we, developers, find some bugs in our logs, this sometimes is worse than a dragon fight!
Let's start with the basics. We have this order of severity of logs, from most detailed to no detail at all:
TRACE
DEBUG
INFO
WARN
ERROR
FATAL
OFF
The default severity log for your classes is INFO
. You don't need to change your configuration file (application.yaml).
logging: level: root: INFO
Let's create a sample controller to test some of the severity logs:
@RestController @RequestMapping("/api") public class LoggingController { private static final Logger logger = LoggerFactory.getLogger(LoggingController.class); @GetMapping("/test") public String getTest() { testLogs(); return "Ok"; } public void testLogs() { System.out.println(" ==== LOGS ==== "); logger.error("This is an ERROR level log message!"); logger.warn("This is a WARN level log message!"); logger.info("This is an INFO level log message!"); logger.debug("This is a DEBUG level log message!"); logger.trace("This is a TRACE level log message!"); } }
We can test it with HTTPie or any other REST client:
$ http GET :8080/api/test
HTTP/1.1 200
Ok
Checking in Spring Boot logs, we will see something like this:
==== LOGS ====
2024-09-08T20:50:15.872-03:00 ERROR 77555 --- [nio-8080-exec-5] LoggingController : This is an ERROR level log message!
2024-09-08T20:50:15.872-03:00 WARN 77555 --- [nio-8080-exec-5] LoggingController : This is a WARN level log message!
2024-09-08T20:50:15.872-03:00 INFO 77555 --- [nio-8080-exec-5] LoggingController : This is an INFO level log message!
If we need to change it to DEBUG
all my com.boaglio
classes, we need to add this info to the application.yaml file and restart the application:
logging: level: com.boaglio: DEBUG
Now repeating the test, we will see a new debug line:
==== LOGS ====
2024-09-08T20:56:35.082-03:00 ERROR 81780 --- [nio-8080-exec-1] LoggingController : This is an ERROR level log message!
2024-09-08T20:56:35.082-03:00 WARN 81780 --- [nio-8080-exec-1] LoggingController : This is a WARN level log message!
2024-09-08T20:56:35.083-03:00 INFO 81780 --- [nio-8080-exec-1] LoggingController : This is an INFO level log message!
2024-09-08T20:56:35.083-03:00 DEBUG 81780 --- [nio-8080-exec-1] LoggingController : This is a DEBUG level log message!
This is good, but sometimes we are running in production and we need to change from INFO
to TRACE
, just for quick research.
This is possible with the LoggingSystem
class. Let's add to our controller a POST API to change all logs to TRACE
:
@Autowired private LoggingSystem loggingSystem; @PostMapping("/trace") public void setLogLevelTrace() { loggingSystem.setLogLevel("com.boaglio",LogLevel.TRACE); logger.info("TRACE active"); testLogs(); }
We are using the LoggingSystem.setLogLevel
method, changing all logs from the package com.boaglio
to TRACE
.
Let's try to call out POST API to enable TRACE
:
$ http POST :8080/api/trace
HTTP/1.1 200
Now we can check that the trace was finally enabled:
2024-09-08T21:04:03.791-03:00 INFO 82087 --- [nio-8080-exec-3] LoggingController : TRACE active
==== LOGS ====
2024-09-08T21:04:03.791-03:00 ERROR 82087 --- [nio-8080-exec-3] LoggingController : This is an ERROR level log message!
2024-09-08T21:04:03.791-03:00 WARN 82087 --- [nio-8080-exec-3] LoggingController : This is a WARN level log message!
2024-09-08T21:04:03.791-03:00 INFO 82087 --- [nio-8080-exec-3] LoggingController : This is an INFO level log message!
2024-09-08T21:04:03.791-03:00 DEBUG 82087 --- [nio-8080-exec-3] LoggingController : This is a DEBUG level log message!
2024-09-08T21:04:03.791-03:00 TRACE 82087 --- [nio-8080-exec-3] LoggingController : This is a TRACE level log message!
And a bonus tip here, to enable DEBUG
or TRACE
just for the Spring Boot framework (which is great sometimes to understand what is going on under the hood), we can simply add this to our application.yaml:
debug:true
or
trace: true
Let the game of trace begin!
Opinions expressed by DZone contributors are their own.
Comments