Wednesday, January 10, 2018

Logging with logback

Greetings!

It is important to add proper loggers into our application because when something bad happens, it is the way to gather related information.

There are multiple libraries for logging in Java like JUL, log4j, logback. Spring uses logback as the default library.
When we add any starter dependency that will add starter-logging as well since it depends transitively on the logging starter.

howto-configure-logback-for-logging

Complete source code, todoapp
$ git clone https://github.com/slmanju/todoapp.git
$ cd todoapp
$ gradle clean bootrun

Log Levels

Log messages can be used in several levels.
ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF
If WARN is enabled, then WARN, ERROR, FATAL will be logged

Appenders

Appenders responsible for writing log statements.
  • ConsoleAppender – writes messages to the system console
  • FileAppender – appends messages to a file
  • RollingFileAppender – extends the FileAppender with the ability to roll over log files
  • SMTPAppender – sends log messages in an email, by default only for ERROR messages
  • DBAppender – adds log events to a database
  • SiftingAppender – separates logs based on a run time attribute

Layouts and Encoders

Layouts and encoders transform a log message to the desired output format.
  • The PatternLayout - This layout creates a String from a log message based on a conversion pattern.
  • The HTMLLayout - The HTMLLayout displays log data in an HTML table format, to which you can add custom styles.

Spring

When you start a spring boot application you can see something like below in the console which comes from the spring boot default configurations.
2018-01-10 13:52:40.579  INFO 16885 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-01-10 13:52:41.157  INFO 16885 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)

Customize logback

We can modify that using application.properties
<!-- application.properties file -->

Pattern

%d – Used to output the date of the logging event.
%thread – outputs the name of the thread that the log message occurred in.
$-5level – outputs the logging level of the log message.
%logger{36} – Outputs the name of the logger
%M – Outputs the method name where the logging request was issued.
%msg – Outputs the application-supplied message
%n – line break

logback-conversion-word

For a simple application this will be enough. But for a large application we need more control over logging. So we need to add logback.xml file which will override default configurations.
To include Spring Boot’s configuration, you can add the following inside the tags.

<include resource="org/springframework/boot/logging/logback/base.xml"></include>

WARNING : Not using additivity="false" will cause the message to be printed out twice due to the root log appender and the class level appender both writing to the log

<!-- logback.xml file -->

Using Spring's profile feature we can create separate configuration between environments.

# spring.profiles.active = dev

Complete source code, todoapp
Read the full tutorial series, spring-rest


No comments:

Post a Comment