Maven Dependencies:
For slf4j add following dependency to your pom.
This is for slf4j which is a facade, an API, which should be implemented by a
logging framework. As a logging framework logback will be used.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
Add following dependency for logback.
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
And finally if there are libraries or
legacy codes that are using log4j as logging system, add following dependency.
This library will forward log4j logging requests to slf4j.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
Logging Configuration File:
Put logback.xml to class path.
<root level="DEBUG">
<appender-ref ref="FILE"/>
<appender-ref ref="STDOUT"/>
</root>
Add root logger definition. Assign a
logging level as appropriate. Now define the referenced appenders, which are
logging destinations.
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover
-->
<fileNamePattern>application.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days'
worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread]
%logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
Encoder pattern is what log statement will
look like on output. Now add a console appender for easier debugging if you are
using an IDE with console screen.
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS}
[%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
Now necessary configuration to collect logs
on application.log file and console is ready. Let’s add one more logger and
appender for better analysis of executing system.
<logger name="com.opensource.tr.myapplication"
additivity="false">
<appender-ref ref="FILE-REDUCED"/>
</logger>
<appender name="FILE-REDUCED" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>application-reduced.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover
-->
<fileNamePattern>application-reduced.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days'
worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread]
%logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
Additivity : Note the additivity attribute. False means produced logs won’t be
sent to upper level loggers. In other words, logs produced from “com.opensource.tr.myapplication” package and
below won’t be seen in application.log file. They will only be included in
application-reduced.log file.
Effective
Level : Note that logging level is not specified.
It will be inherited from the first defined logging level on its upper
hierarchy. In this case it is the root logger and its level is debug.
As a result, the logger’s effective logging level will be debug.
If it is favorable to change logging
configuration without application restart add following definition to
configuration root to make logback regularly scan for configuration file
changes.
<configuration scan="true" scanPeriod="30
seconds">
This line means
configuration file will be scanned every 30 seconds and if a change is detected
logging system will be reconfigured. Remember default time unit for period is milliseconds.
To enable JMX configuration adding below
line to configuration file is enough:
<jmxConfigurator />
Logging Tips:
-
Logging
levels are from lowest to highest:
TRACE, DEBUG, INFO, WARN and ERROR
o
ERROR is good
for reporting application exceptions that substantially effect application
flow.
o
WARN is good
for application exceptions that are not critical for application flow.
o
INFO is good
for reporting for execution of important
points of application, like successfully obtaining a JDBC connection or reading
confiugration file.
o
DEBUG is good
for creating information for later debugging of system in case of system
errros.
o
TRACE level
is not a commonly used logging level.
-
Instead of
using so called ‘Guarded Logging’ use slf4j parameterized logging approach.
Ä°nstead of :
if(logger.isDebugEnabled()) {
logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
}
logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
}
Use :
Object entry = new SomeObject(); logger.debug("The entry is {}.", entry);
So that parameter constraction cost of
ineligible-to-logging statements are avoided.
And code doesn’t look messy.
Complete logack.xml file:
<configuration scan="true" scanPeriod="30
seconds">
<contextName>MyApplication</contextName>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the
type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS}
[%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily
rollover -->
<fileNamePattern>application.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days'
worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date %level
[%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE-REDUCED" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>application-reduced.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily
rollover -->
<fileNamePattern>application-reduced.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days'
worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date %level
[%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE"/>
<appender-ref ref="STDOUT"/>
</root>
<logger name="com.opensource.tr.myapplication" level="DEBUG" additivity="false">
<appender-ref ref="FILE-REDUCED"/>
</logger>
</configuration>
Comments
Post a Comment