Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

REASONS TO USE LOGBACK

No description
by

Dajian Dong

on 29 August 2014

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of REASONS TO USE LOGBACK

REASONS TO USE LOGBACK
What is Logback?
Intended as a successor to the popular log4j project.

Divided into three modules, logback-core, logback-classic and logback-access.

Natively implements the SLF4J API so that you can readily switch back and forth between logback and other logging frameworks such as log4j or java.util.logging.
Projects rely on Logback
Akka
Apache Airvata
Apache Camel
Apache Cocoon 3
Apache Jackrabbit
Apache OpenMeetings
Apache S4
Apache Syncope
Apache Tobago
Artifactory
Citizen Intelligence Agency
dCache
Dropwizard
Picking up where log4j leaves off
Faster implementation
logback internals have been re-written to perform about
ten times faster
on certain critical execution paths.

Not only are logback components faster, they have
a smaller memory footprint
as well.
Geomajas
Gradle
Grinder
Gyrex
JAOP
JMX Monitor
JWebUnit
Lift
Metrics
OpenGDA
OpenRDF
OpenTSDB
Play Framework
Red5
Scalate
Scalatra
Shibboleth
Sonar
SpringSource dm Server
StreamBase
Virgo Web Server
XtremeMP
Xuggler
Xwiki
Zabbix
logback-classic speaks SLF4J natively
If you need to switch to log4j or to JUL (java.util.logging), you can do so
by replacing one jar file
with another.

You will
not need to touch your code
logging via the SLF4J API. This can drastically reduce the work involved in switching logging frameworks.
logback-classic.x.x.x.jar
logback-core.x.x.x.jar
slf4j-api-x.x.x.jar
Configuration in logback
1. Logback tries to find a file called
logback.groovy
in the classpath.

2. If no such file is found, logback tries to find a file called
logback-test.xml
in the classpath.

3. If no such file is found, it checks for the file
logback.xml
in the classpath..

4. If neither file is found, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.
Conditional processing of configuration files
To avoid duplication, logback supports conditional processing of configuration files with the help of
<if>, <then> and <else>
elements so that a single configuration file can adequately target several environments.
<configuration debug="true">

<if condition='property("ENV").equals("PROD")'>
<then>
<property name="path" value="foooooo"/>
</then>
<else>
<property name="path" value="baaaaaaaar"/>
</else>
</if>

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>
${path}
/conditional.log</file>
<encoder>
<pattern>%d %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>

<if condition='property("ENV").equals("DEV")'>
<then>
<logger name="com.vmware.cam" level="DEBUG" />
</then>
</if>

<root level="WARN">
<appender-ref ref="FILE" />
</root>
</configuration>
Configuring Appenders
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator>
<expression>
return message.contains("billing");
</expression>
</evaluator>
<OnMismatch>NEUTRAL</OnMismatch>

<OnMatch>DENY</OnMatch>
</filter>
</appender>
Filters
SiftingAppender is an amazingly versatile appender. It can be used to separate (or sift) logging according to any given runtime attribute.
SiftingAppender

<appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator class="
com.vmware.cam.Discriminator
">
<key>userid</key>
<defaultValue>unknown</defaultValue>
</discriminator>
<sift>
<appender name="FILE-${userid}" class="ch.qos.logback.core.FileAppender">

<file>${userid}.log</file>
<append>false</append>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d [%thread] %level %mdc %logger{35} - %msg%n</pattern>
</layout>
</appender>
</sift>
</appender>
Turbo Filters
ThresholdFilter
EvaluatorFilter
...
logger.debug("Hello, my name is" + username + ", I am " + age + " years old.");

V.S.

logger.debug("Hello, my name is {}, I am {} years old", username, age);
Parameterized logging
Access log:
The log generated when a user accesses a web-page on a web
server.

Logback-access integrates seamlessly with Jetty and Tomcat

Logback Access
127.0.0.1 - - 22/01/2007:14:35:40 +0100 GET /logback-demo/ViewStatii.do HTTP/1.1 200 3660
127.0.0.1 - - 22/01/2007:14:35:41 +0100 GET /logback-demo/index.jsp HTTP/1.1 200 2389
127.0.0.1 - - 22/01/2007:14:35:42 +0100 GET /logback-demo/lastLog/ HTTP/1.1 200 948
127.0.0.1 - - 22/01/2007:14:35:42 +0100 GET /logback-demo/index.jsp HTTP/1.1 200 2389
127.0.0.1 - - 22/01/2007:14:35:43 +0100 GET /logback-demo/prime.jsp HTTP/1.1 200 1296
127.0.0.1 - - 22/01/2007:14:35:44 +0100 GET /logback-demo/index.jsp HTTP/1.1 200 2389
127.0.0.1 - - 22/01/2007:14:35:45 +0100 GET /logback-demo/lottery.jsp HTTP/1.1 200 1209
127.0.0.1 - - 22/01/2007:14:35:46 +0100 GET /logback-demo/index.jsp HTTP/1.1 200 2389
127.0.0.1 - - 22/01/2007:14:35:48 +0100 GET /logback-demo/reload.jsp HTTP/1.1 200 1335
127.0.0.1 - - 22/01/2007:14:35:49 +0100 GET /logback-demo/index.jsp HTTP/1.1 200 2389
127.0.0.1 - - 22/01/2007:14:35:54 +0100 GET /logback-demo/login.jsp HTTP/1.1 200 1214
127.0.0.1 - - 22/01/2007:14:35:55 +0100 GET /logback-demo/Logout.do HTTP/1.1 200 1000
JMX

Graceful recovery from I/O failures

Automatic removal of old log archives

Automatic compression of archived log files

...
Others
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.13</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>
The Simple Logging Facade for Java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

private static final
Logger
logger =
LoggerFactory.getLogger
(MyClassName.class);
Automatic reloading of configuration files
Logback-classic can
automatically reload
its configuration file upon modification.
<configuration scan="true">
...
</configuration>
<configuration scan="true"
scanPeriod="30 seconds"
>
...
</configuration>
Why?
src
main
resources
logback.xml
test
resources
logback-test.xml
<configuration debug="true">

<appender name="
FILE_WORKFLOW
" class="ch.qos.logback.core.FileAppender">
<file>${path}/
workflow.log
</file>
<encoder>
<pattern>%d %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>

<appender name="
FILE_CAM
" class="ch.qos.logback.core.FileAppender">
<file>${path}/
cam.log
</file>
<encoder>
<pattern>%d %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>

<logger name=
"com.vmware.cam.workflow"
additivity="false"
>

<appender-ref ref="FILE_WORKFLOW" />
</logger>

<root level="WARN">
<appender-ref ref="FILE_VNAS" />
</root>
</configuration>
<configuration>
<contextName>myAppName</contextName>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d
%contextName
[%t] %level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
File Inclusion
<configuration>
<include file="src/main/java/chapters/configuration/includedConfig.xml"/>

<root level="DEBUG">
<appender-ref ref="includedConsole" />
</root>

</configuration>
<included>
<appender name="includedConsole" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>"%d - %m%n"</pattern>
</encoder>
</appender>
</included>
Other Appenders
DBAppender
SMTPAppender
SocketAppender
ServerSocketAppender
AsyncAppender
RollingFileAppender
FileAppender
ConsoleAppender
OutputStreamAppender
Write your owen appenders...
Multiple FileAppender instances running on multiple JVMs can safely write to
the same log file
.

Setting the context name is a simple and straightforward method in order to distinguish between multiple applications logging to the same target.
Prudent Mode
V.S.
import org.apache.log4j.Logger;

private static final Logger logger =

Logger.getLogger
(MyClassName.class);
Q & A
Full transcript