[logback-dev] svn commit: r2147 - in logback/trunk: logback-examples logback-examples/src/main/java/chapter11 logback-site/src/site/pages/manual
noreply.ceki at qos.ch
noreply.ceki at qos.ch
Mon Feb 9 21:20:15 CET 2009
Author: ceki
Date: Mon Feb 9 21:20:15 2009
New Revision: 2147
Added:
logback/trunk/logback-examples/src/main/java/chapter11/
logback/trunk/logback-examples/src/main/java/chapter11/Log4jMain.java
logback/trunk/logback-examples/src/main/java/chapter11/LogbackMain.java
logback/trunk/logback-examples/src/main/java/chapter11/TrivialLog4jAppender.java
logback/trunk/logback-examples/src/main/java/chapter11/TrivialLog4jLayout.java
logback/trunk/logback-examples/src/main/java/chapter11/TrivialLogbackAppender.java
logback/trunk/logback-examples/src/main/java/chapter11/TrivialLogbackLayout.java
logback/trunk/logback-examples/src/main/java/chapter11/log4jTrivial.properties
logback/trunk/logback-examples/src/main/java/chapter11/logback-trivial.xml
logback/trunk/logback-site/src/site/pages/manual/migrationFromLog4j.html
Modified:
logback/trunk/logback-examples/pom.xml
logback/trunk/logback-site/src/site/pages/manual/appenders.html
logback/trunk/logback-site/src/site/pages/manual/index.html
logback/trunk/logback-site/src/site/pages/manual/menu.js
Log:
Added a chapter in the documentation about migrating log4j layouts
and log4j appenders to logback as requested in LBSITE-27
Modified: logback/trunk/logback-examples/pom.xml
==============================================================================
--- logback/trunk/logback-examples/pom.xml (original)
+++ logback/trunk/logback-examples/pom.xml Mon Feb 9 21:20:15 2009
@@ -1,105 +1,110 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <parent>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-parent</artifactId>
- <version>0.9.15-SNAPSHOT</version>
- </parent>
-
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-examples</artifactId>
- <version>${parent.version}</version>
- <packaging>jar</packaging>
- <name>Logback Examples Module</name>
-
- <url>http://logback.qos.ch</url>
- <description>
- Logback: the generic, reliable, fast and flexible logging
- library for Java.
- </description>
- <licenses>
- <license>
- <name>GNU Lesser General Public License</name>
- <url>http://www.gnu.org/licenses/lgpl.html</url>
- </license>
- </licenses>
-
- <dependencies>
-
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-core</artifactId>
- </dependency>
-
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-classic</artifactId>
- </dependency>
-
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-access</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-ext</artifactId>
+ <parent>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-parent</artifactId>
+ <version>0.9.15-SNAPSHOT</version>
+ </parent>
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-examples</artifactId>
+ <version>${parent.version}</version>
+ <packaging>jar</packaging>
+ <name>Logback Examples Module</name>
+
+ <url>http://logback.qos.ch</url>
+ <description>
+ Logback: the generic, reliable, fast and flexible logging library for Java.
+ </description>
+ <licenses>
+ <license>
+ <name>GNU Lesser General Public License</name>
+ <url>http://www.gnu.org/licenses/lgpl.html</url>
+ </license>
+ </licenses>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-core</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-access</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-ext</artifactId>
<version>${slf4j.version}</version>
- </dependency>
+ </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <scope>compile</scope>
- <optional>true</optional>
- </dependency>
-
- </dependencies>
-
- <build>
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <filtering>true</filtering>
- </resource>
- </resources>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.5</source>
- <target>1.5</target>
- </configuration>
- </plugin>
-
- <plugin>
- <artifactId>maven-antrun-plugin</artifactId>
- <executions>
- <execution>
- <phase>package</phase>
- <configuration>
- <tasks>
- <property name="slf4jJAR"
- value="${settings.localRepository}/org/slf4j/slf4j-api/${slf4j.version}/slf4j-api-${slf4j.version}.jar" />
- <echo>Copying ${slf4jJAR} to lib/</echo>
- <delete>
- <fileset dir="lib/"
- includes="slf4j-*SNAPSHOT.jar" />
- </delete>
- <copy file="${slf4jJAR}" todir="lib/" />
- </tasks>
- </configuration>
- <goals>
- <goal>run</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>1.2.14</version>
+ </dependency>
+
+
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <scope>compile</scope>
+ <optional>true</optional>
+ </dependency>
+
+ </dependencies>
+
+ <build>
+ <resources>
+ <resource>
+ <directory>src/main/resources</directory>
+ <filtering>true</filtering>
+ </resource>
+ </resources>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <artifactId>maven-antrun-plugin</artifactId>
+ <executions>
+ <execution>
+ <phase>package</phase>
+ <configuration>
+ <tasks>
+ <property name="slf4jJAR"
+ value="${settings.localRepository}/org/slf4j/slf4j-api/${slf4j.version}/slf4j-api-${slf4j.version}.jar" />
+ <echo>Copying ${slf4jJAR} to lib/</echo>
+ <delete>
+ <fileset dir="lib/" includes="slf4j-*SNAPSHOT.jar" />
+ </delete>
+ <copy file="${slf4jJAR}" todir="lib/" />
+ </tasks>
+ </configuration>
+ <goals>
+ <goal>run</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
</project>
\ No newline at end of file
Added: logback/trunk/logback-examples/src/main/java/chapter11/Log4jMain.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-examples/src/main/java/chapter11/Log4jMain.java Mon Feb 9 21:20:15 2009
@@ -0,0 +1,30 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ *
+ * Copyright (C) 2000-2009, QOS.ch
+ *
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package chapter11;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+
+/**
+ * A minimal application making use of log4j and TrivialLog4jAppender.
+ *
+ * @author Ceki Gülcü
+ *
+ */
+public class Log4jMain {
+
+ static Logger logger = Logger.getLogger(Log4jMain.class);
+
+ public static void main(String[] args) {
+ PropertyConfigurator.configure("src/main/java/chapter11/log4jTrivial.properties");
+ logger.debug("Hello world");
+ }
+
+}
Added: logback/trunk/logback-examples/src/main/java/chapter11/LogbackMain.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-examples/src/main/java/chapter11/LogbackMain.java Mon Feb 9 21:20:15 2009
@@ -0,0 +1,44 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ *
+ * Copyright (C) 2000-2009, QOS.ch
+ *
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package chapter11;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.joran.JoranConfigurator;
+import ch.qos.logback.core.joran.spi.JoranException;
+import ch.qos.logback.core.util.StatusPrinter;
+
+/**
+ * A minimal application making use of logback-classic. It uses the
+ * configuration file logback-trivial.xml which makes use of
+ * TivialLogbackAppender.
+ *
+ * @author Ceki Gülcü
+ */
+public class LogbackMain {
+
+ static Logger logger = LoggerFactory.getLogger(LogbackMain.class);
+
+ public static void main(String[] args) throws JoranException {
+ LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
+
+ JoranConfigurator configurator = new JoranConfigurator();
+ configurator.setContext(lc);
+ lc.reset();
+ configurator.doConfigure("src/main/java/chapter11/logback-trivial.xml");
+ StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
+
+
+ logger.debug("Hello world");
+ }
+
+}
Added: logback/trunk/logback-examples/src/main/java/chapter11/TrivialLog4jAppender.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-examples/src/main/java/chapter11/TrivialLog4jAppender.java Mon Feb 9 21:20:15 2009
@@ -0,0 +1,30 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ *
+ * Copyright (C) 2000-2009, QOS.ch
+ *
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package chapter11;
+
+import org.apache.log4j.AppenderSkeleton;
+import org.apache.log4j.spi.LoggingEvent;
+
+public class TrivialLog4jAppender extends AppenderSkeleton {
+
+ protected void append(LoggingEvent loggingevent) {
+ String s = this.layout.format(loggingevent);
+ System.out.println(s);
+ }
+
+ public void close() {
+ // nothing to do
+ }
+
+ public boolean requiresLayout() {
+ return true;
+ }
+
+}
Added: logback/trunk/logback-examples/src/main/java/chapter11/TrivialLog4jLayout.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-examples/src/main/java/chapter11/TrivialLog4jLayout.java Mon Feb 9 21:20:15 2009
@@ -0,0 +1,39 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ *
+ * Copyright (C) 2000-2009, QOS.ch
+ *
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package chapter11;
+
+import org.apache.log4j.Layout;
+import org.apache.log4j.spi.LoggingEvent;
+
+
+/**
+ *
+ * A very simple log4j layout which formats a logging event
+ * by returning the message contained therein.
+ *
+ * @author Ceki Gülcü
+ *
+ */
+public class TrivialLog4jLayout extends Layout {
+
+ public void activateOptions() {
+ // there are no options to activate
+ }
+
+ public String format(LoggingEvent loggingEvent) {
+ return loggingEvent.getRenderedMessage();
+ }
+
+ @Override
+ public boolean ignoresThrowable() {
+ return true;
+ }
+
+}
Added: logback/trunk/logback-examples/src/main/java/chapter11/TrivialLogbackAppender.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-examples/src/main/java/chapter11/TrivialLogbackAppender.java Mon Feb 9 21:20:15 2009
@@ -0,0 +1,35 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ *
+ * Copyright (C) 2000-2009, QOS.ch
+ *
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package chapter11;
+
+import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.core.AppenderBase;
+
+public class TrivialLogbackAppender extends AppenderBase<LoggingEvent> {
+
+ @Override
+ public void start() {
+ if (this.layout == null) {
+ addError("No layout set for the appender named [" + name + "].");
+ return;
+ }
+ super.start();
+ }
+
+ @Override
+ protected void append(LoggingEvent loggingevent) {
+ // note that AppenderBase.doAppend will invoke this method only if
+ // this appender was successfully started.
+
+ String s = this.layout.doLayout(loggingevent);
+ System.out.println(s);
+ }
+
+}
Added: logback/trunk/logback-examples/src/main/java/chapter11/TrivialLogbackLayout.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-examples/src/main/java/chapter11/TrivialLogbackLayout.java Mon Feb 9 21:20:15 2009
@@ -0,0 +1,29 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ *
+ * Copyright (C) 2000-2009, QOS.ch
+ *
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package chapter11;
+
+import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.core.LayoutBase;
+
+
+/**
+ *
+ * A very simple logback-classic layout which formats a logging event
+ * by returning the message contained therein.
+ *
+ * @author Ceki Gülcü
+ *
+ */
+public class TrivialLogbackLayout extends LayoutBase<LoggingEvent> {
+
+ public String doLayout(LoggingEvent loggingEvent) {
+ return loggingEvent.getMessage();
+ }
+}
Added: logback/trunk/logback-examples/src/main/java/chapter11/log4jTrivial.properties
==============================================================================
--- (empty file)
+++ logback/trunk/logback-examples/src/main/java/chapter11/log4jTrivial.properties Mon Feb 9 21:20:15 2009
@@ -0,0 +1,3 @@
+log4j.rootLogger=DEBUG, TRIVIAL
+log4j.appender.TRIVIAL=chapter11.TrivialLog4jAppender
+log4j.appender.TRIVIAL.layout=chapter11.TrivialLog4jLayout
\ No newline at end of file
Added: logback/trunk/logback-examples/src/main/java/chapter11/logback-trivial.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-examples/src/main/java/chapter11/logback-trivial.xml Mon Feb 9 21:20:15 2009
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<configuration>
+ <appender name="TRIVIAL" class="chapter11.TrivialLogbackAppender">
+ <layout class="chapter11.TrivialLogbackLayout"/>
+ </appender>
+ <root level="DEBUG">
+ <appender-ref ref="TRIVIAL"/>
+ </root>
+</configuration>
\ No newline at end of file
Modified: logback/trunk/logback-site/src/site/pages/manual/appenders.html
==============================================================================
--- logback/trunk/logback-site/src/site/pages/manual/appenders.html (original)
+++ logback/trunk/logback-site/src/site/pages/manual/appenders.html Mon Feb 9 21:20:15 2009
@@ -3238,8 +3238,6 @@
static int DEFAULT_LIMIT = 16;
int counter = 0;
int limit = DEFAULT_LIMIT;
-
- private Layout<LoggingEvent> layout;
public CountingConsoleAppender() {
}
@@ -3275,38 +3273,37 @@
counter++;
}
- public Layout<LoggingEvent> getLayout() {
- return layout;
- }
-
- public void setLayout(Layout<LoggingEvent> layout) {
- this.layout = layout;
- }
}</p>
<p>The <code>start()</code> method checks for the presence of a
- <code>Layout</code>. In case none is found, the appender is not
- started.
+ <code>Layout</code>. In case the layout is not set, the appender
+ fails to start with an error message.
</p>
<p>This custom appender illustrates a two points:
</p>
<ul>
- <li>All properties that follow the setter/getter JavaBeans
- conventions are handled transparently. The <code>start()</code>
- method, that is called automatically, has the responsability to
- check that the given properties are coherent.
+
+ <li>All properties that follow the setter/getter JavaBeans
+ conventions are handled transparently. The <code>start()</code>
+ method, which is called automatically during logback
+ configuration, has the responsibility to verify that the various
+ properties of the appender are coherent.
</li>
+
<li>The <code>AppenderBase.doAppend()</code> method invokes the
- append() method of its derived classes where actual output
- operations occur. It is in this method that appenders format
- events by invoking their layouts.
+ append() method of its derived classes. Actual output
+ operations occur in the <code>append</code>() method. In
+ particular, it is in this method that appenders format events by
+ invoking their layouts.
</li>
</ul>
- <p>The <code>CountingConsoleAppender</code> can be configured like
- any appender. See sample file
+ <p>The <a
+ href="../xref/chapter4/CountingConsoleAppender.html"><code>CountingConsoleAppender</code></a>
+ can be configured like any appender. See sample configuration
+ file
<em>logback-examples/src/main/java/chapter4/countingConsole.xml</em>
for an example.
</p>
Modified: logback/trunk/logback-site/src/site/pages/manual/index.html
==============================================================================
--- logback/trunk/logback-site/src/site/pages/manual/index.html (original)
+++ logback/trunk/logback-site/src/site/pages/manual/index.html Mon Feb 9 21:20:15 2009
@@ -24,11 +24,22 @@
<h2>The logback manual</h2>
+ <div class="highlight">
+ <p>
+ If you wish to print chapters in this document, we recommend
+ that you do so using <a
+ href="http://www.getfirefox.com">Firefox 2</a>, with <em>Adapt to
+ page size</em> enabled, or <a
+ href="http://www.opera.com">Opera</a>.
+ </p>
+ </div>
+
<p>The complete logback manual documents the latest version of
- logback framework. In over 140 pages and dozens of concrete
+ logback framework. In over 150 pages and dozens of concrete
examples, it covers both basic and advanced logback features:
</p>
+
<div>
<ul>
<li><p>the overall logback architecture</p></li>
@@ -38,28 +49,11 @@
<li><p>layouts</p></li>
<li><p>filters</p></li>
<li><p>mapped diagnostic contexts</p></li>
- <li><p>logback default initialization</p></li>
- <li><p>logback in Servlet Containers</p></li>
+ <li><p>joran, logback's configuration system</p></li>
</ul>
</div>
-
- <div class="highlight">
- <p>
- If you wish to print chapters in this document, we recommend
- that you do so using <a
- href="http://www.getfirefox.com">Firefox 2</a>, with <em>Adapt to
- page size</em> enabled, or <a
- href="http://www.opera.com">Opera</a>.
- </p>
- <p>
- To run the examples provided in this book, you might have
- to run the provided script to setup your classpath. The scripts
- can be found in the logback distributions, inside the <em>logback-examples</em>
- directory.
- </p>
- </div>
-
+
<p>The logback manual describes the logback API in considerable
detail, including its features and design rationale. Authored by
Ceki Gülcü and Sébastien Pennec, the main
@@ -69,6 +63,8 @@
the aid of introductory material and many examples, new users
should quickly come up to speed.
</p>
+
+
<div>
<p>Without further ado, here are the contents of the manual:</p>
@@ -112,6 +108,10 @@
<a href="onJoran.html"><b>Chapter 10: Joran</b></a>
</p></li>
+ <li><p>
+ <a href="migrationFromLog4j.html"><b>Chapter 11: Migration from log4j</b></a>
+ </p></li>
+
</ul>
</div>
Modified: logback/trunk/logback-site/src/site/pages/manual/menu.js
==============================================================================
--- logback/trunk/logback-site/src/site/pages/manual/menu.js (original)
+++ logback/trunk/logback-site/src/site/pages/manual/menu.js Mon Feb 9 21:20:15 2009
@@ -10,3 +10,4 @@
document.write('<p class="menu"><a href="loggingSeparation.html"><b>Ch8: Logging Separation</b></a>');
document.write('<p class="menu"><a href="jmxConfig.html"><b>Ch9: JMX Configurator</b></a>');
document.write('<p class="menu"><a href="onJoran.html"><b>Ch10: Joran</b></a>');
+document.write('<p class="menu"><a href="migrationFromLog4j.html"><b>Ch11: Migration from log4j</b></a>');
Added: logback/trunk/logback-site/src/site/pages/manual/migrationFromLog4j.html
==============================================================================
--- (empty file)
+++ logback/trunk/logback-site/src/site/pages/manual/migrationFromLog4j.html Mon Feb 9 21:20:15 2009
@@ -0,0 +1,203 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
+ <title>Migration from log4j</title>
+ <link rel="stylesheet" type="text/css" href="../css/common.css" />
+ <link rel="stylesheet" type="text/css" href="../css/screen.css" media="screen" />
+ <link rel="stylesheet" type="text/css" href="../css/_print.css" media="print" />
+
+ </head>
+ <body>
+ <script type="text/javascript">prefix='../'</script>
+ <script src="../templates/header.js" type="text/javascript"></script>
+ <div id="left">
+ <script src="../templates/left.js" type="text/javascript"></script>
+ </div>
+ <div id="right">
+ <script src="menu.js" type="text/javascript"></script>
+ </div>
+ <div id="content">
+
+ <h2>Migration from log4j</h2>
+
+
+ <p>This chapter deals with the topic of migrating custom log4j
+ components such as appenders or layouts to logback-classic.
+ </p>
+
+ <p>Software which merely invokes log4j client API, that is the
+ <code>Logger</code> or <code>Category</code> classes in
+ <code>org.apache.log4j</code> package, can be automatically
+ migrated to use SLF4J via the <a
+ href="http://www.slf4j.org/migrator.html">SLF4J migrator
+ tool</a>.
+ </p>
+
+ <p>From a broader perspective, log4j and logback-classic are
+ closely related. The core components, such as loggers, appenders
+ and layouts exist in both frameworks and serve identical
+ purposes. Similarly, the most important internal data-structure,
+ namely <code>LoggingEvent</code> exists in both frameworks with
+ rather similar but non-identical implementations. Most of the
+ changes requried in migrating log4j components to logback-classic
+ are related to differences in implementation of the
+ <code>LoggingEvent</code> class. Rest assured, these differences
+ are rather limited. If in spite of your best efforts you are
+ unable to migrate any given log4j component to logback-classic, do
+ post a question on the <a href="../mailinglist.html">logback-dev
+ mailing list</a>. A logback developper should be able to provide
+ guidance.
+ </p>
+
+
+ <h3>Migrating a log4j layout</h3>
+
+ <p>Let us begin by migrating a hypothetcical and trivially simple
+ log4j layout named <a
+ href="../xref/chapter11/TrivialLog4jLayout.html">TrivialLog4jLayout</a>
+ which returns the message contained in a logging events as the
+ formatted message. Here is the code.
+ </p>
+
+
+ <p class="source">package chapter11;
+
+import org.apache.log4j.Layout;
+import org.apache.log4j.spi.LoggingEvent;
+
+public class TrivialLog4jLayout extends Layout {
+
+ public void activateOptions() {
+ // there are no options to activate
+ }
+
+ public String format(LoggingEvent loggingEvent) {
+ return loggingEvent.getRenderedMessage();
+ }
+
+ public boolean ignoresThrowable() {
+ return true;
+ }
+}</p>
+
+ <p>The logback-classic equivalent named <a
+ href="../xref/chapter11/TrivialLogbackLayout.html">TrivialLogbackLayout</a>
+ would be </p>
+
+ <p class="source">package chapter11;
+
+import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.core.LayoutBase;
+
+public class TrivialLogbackLayout extends <b>LayoutBase<LoggingEvent></b> {
+
+ public String <b>doLayout</b>(LoggingEvent loggingEvent) {
+ return loggingEvent.getMessage();
+ }
+} </p>
+
+ <p>As you can see, in a logback-classic layout, the formatting
+ method is named <code>doLayout</code> instead of
+ <code>format</code>() in log4j. The <code>ignoresThrowable</code>()
+ method is not needed and has no equivalent in logback-classic. Note
+ that a logback-classic layout must extend the
+ <code>LayoutBase<LoggingEvent></code> class.
+ </p>
+
+ <p>The <code>activateOptions</code>() method merits further
+ discussion. In log4j, a layout will have its
+ <code>activateOptions</code>() method invoked by log4j
+ configurators, that is <code>PropertyConfigurator</code> or
+ <code>DOMConfigurator</code> just after all the options of the
+ layout have been set. Thus, the layout will have an oppurtunity to
+ check that its options are coherent and if so, proceed to fully
+ intialize itself.</p>
+
+
+ <p>In logback-classic, layouts must implement the <a
+ href="../xref/ch/qos/logback/core/spi/LifeCycle.html">LifeCycle</a>
+ interface which includes a method called <code>start</code>(). The
+ <code>start</code>() method is the equivalent of log4j's
+ <code>activateOptions</code>() method.
+ </p>
+
+ <h3>Migrating a log4j appender</h3>
+
+ <p>Migrating an appender is quite similar to migrating layout. Here
+ is a trivially simple appender called <a
+ href="../xref/chapter11/TrivialLog4jLayout.html">TrivialLog4jLayout</a>
+ which writes on the console the string returned by its layout.</p>
+
+ <p class="source">package chapter11;
+
+import org.apache.log4j.AppenderSkeleton;
+import org.apache.log4j.spi.LoggingEvent;
+
+
+public class TrivialLog4jAppender extends AppenderSkeleton {
+
+ protected void append(LoggingEvent loggingevent) {
+ String s = this.layout.format(loggingevent);
+ System.out.println(s);
+ }
+
+ public void close() {
+ // nothing to do
+ }
+
+ public boolean requiresLayout() {
+ return true;
+ }
+}</p>
+
+ <p>The logback-classic equivalent named <a
+ href="../xref/chapter11/TrivialLogbackAppender.html">TrivialLogbackAppender</a>
+ would be written as</p>
+
+
+ <p class="source">package chapter11;
+
+import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.core.AppenderBase;
+
+public class TrivialLogbackAppender extends AppenderBase<LoggingEvent> {
+
+ @Override
+ public void start() {
+ if (this.layout == null) {
+ addError("No layout set for the appender named [" + name + "].");
+ return;
+ }
+ super.start();
+ }
+
+ @Override
+ protected void append(LoggingEvent loggingevent) {
+ // note that AppenderBase.doAppend will invoke this method only if
+ // this appender was successfully started.
+
+ String s = this.layout.doLayout(loggingevent);
+ System.out.println(s);
+ }
+}</p>
+
+
+ <p>Comparing the two classes, you should notice that the contents
+ of the <code>append</code>() method remains unchanged. The
+ <code>requiresLayout</code> method is not used in logback and can
+ be removed. In logback, the <code>stop</code>() method is the
+ equivalent of log4j's <code>close</code>() method. However,
+ <code>AppenderBase</code> in logback-classic, contains an nop
+ implementation for <code>stop</code> which is sufficient for the
+ purposes of this trivial appender.
+ </p>
+
+
+
+ <script src="../templates/footer.js" type="text/javascript"></script>
+</div>
+</body>
+</html>
More information about the logback-dev
mailing list