Skip to content

Logback configuration

Rostyslav Khasanov edited this page Sep 29, 2019 · 6 revisions

Spring Boot SLF4j Logback

Logback configuration is located in property files.

There are two property files :

  • application-dev.properties - configuration for developer;
  • application-prod.properties - configuration for server.

The available logging levels in Logback are:

  • OFF (output no logs)
  • ERROR
  • WARN
  • INFO
  • DEBUG
  • TRACE

application-dev.properties:

logging.level.root=info
logging.level.greencity.exception.CustomExceptionHandler=trace
logging.pattern.console=%d{"yyyy/MM/dd HH:mm:ss,SSS"} %magenta([%thread]) %highlight(%-5level) %M\\(%F:%L\\) - %msg%n

This configuration writes logs for all app by log level info (logging.level.root=info) and writes logs into console.

CustomExceptionHandler.class logging by log level trace, for output full stack trace into log.

Some explanations of what each command does (logging.pattern) :

  • %d – outputs the time that the log message occurred in formats that SimpleDateFormat allows.
  • %magenta() – sets the color of the output contained in the brackets to magenta (other colors are available). highlight() – sets the color of the output contained in the brackets depending on the logging level (for example ERROR = red).
  • %thread – outputs the name of the thread that the log message occurred in.
  • %msg – outputs the actual log message.
  • %n – line break.
  • %M\(%F:%L\) - outputs the name of method and class, where log message occurred in.

application-prod.properties:

logging.level.root=info
logging.level.greencity.exception.CustomExceptionHandler=trace
logging.path=logs
logging.file=${logging.path}/log.log
logging.pattern.file=%d{"yyyy/MM/dd HH:mm:ss,SSS"} %magenta([%thread]) %highlight(%-5level) %M\\(%F:%L\\) - %msg%n

This configuration writes logs for all app by log level info (logging.level.root=info) and writes logs into file.

  • Logging path = logs ( all log files is here ).
  • Max size of log file 10MB.
  • When the file is full, new fill will automatically generated. Old file will pack in archive.