| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 2 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 |
|
| 4 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
| 5 |
<head> |
| 6 |
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> |
| 7 |
<title>Chapter 13: Migration from log4j</title> |
| 8 |
<link rel="stylesheet" type="text/css" href="../css/common.css" /> |
| 9 |
<link rel="stylesheet" type="text/css" href="../css/screen.css" media="screen" /> |
| 10 |
<link rel="stylesheet" type="text/css" href="../css/_print.css" media="print" /> |
| 11 |
<link rel="stylesheet" type="text/css" href="../css/prettify.css" media="screen" /> |
| 12 |
</head> |
| 13 |
<body onload="prettyPrint(); decorate();"> |
| 14 |
<script type="text/javascript">prefix='../'</script> |
| 15 |
<script type="text/javascript" src="../js/prettify.js"></script> |
| 16 |
<script type="text/javascript" src="../templates/header.js"></script> |
| 17 |
<script type="text/javascript" src="../js/jquery-min.js"></script> |
| 18 |
<script type="text/javascript" src="../js/decorator.js"></script> |
| 19 |
|
| 20 |
<div id="left"> |
| 21 |
<noscript>Please turn on Javascript to view this menu</noscript> |
| 22 |
<script src="../templates/left.js" type="text/javascript"></script> |
| 23 |
</div> |
| 24 |
<div id="right"> |
| 25 |
<script src="menu.js" type="text/javascript"></script> |
| 26 |
</div> |
| 27 |
<div id="content"> |
| 28 |
|
| 29 |
<h1>Chapter 13: Migration from log4j</h1> |
| 30 |
|
| 31 |
<a href="migrationFromLog4j_ja.html">和訳 (Japanese translation)</a> |
| 32 |
|
| 33 |
<div class="quote"> |
| 34 |
<p><em>The more things change, the more they remain the |
| 35 |
same. </em></p> |
| 36 |
|
| 37 |
<p>—ALPHONSE KARR, <em>Les Guêpes</em></p> |
| 38 |
</div> |
| 39 |
|
| 40 |
<p>This chapter deals with the topic of migrating custom log4j |
| 41 |
components such as appenders or layouts to logback-classic. |
| 42 |
</p> |
| 43 |
|
| 44 |
<p>Software which merely invokes log4j client API, that is the |
| 45 |
<code>Logger</code> or <code>Category</code> classes in |
| 46 |
<code>org.apache.log4j</code> package, can be automatically |
| 47 |
migrated to use SLF4J via the <a |
| 48 |
href="http://www.slf4j.org/migrator.html">SLF4J migrator |
| 49 |
tool</a>. To migrate <em>log4j.property</em> files into its |
| 50 |
logback equivalent, you can use the <a |
| 51 |
href="http://logback.qos.ch/translator/">log4j.properties |
| 52 |
translator</a>. |
| 53 |
</p> |
| 54 |
|
| 55 |
<p>From a broader perspective, log4j and logback-classic are |
| 56 |
closely related. The core components, such as loggers, appenders |
| 57 |
and layouts exist in both frameworks and serve identical |
| 58 |
purposes. Similarly, the most important internal data-structure, |
| 59 |
namely <code>LoggingEvent</code>, exists in both frameworks with |
| 60 |
rather similar but non-identical implementations. Most notably, in |
| 61 |
logback-classic <code>LoggingEvent</code> implements the |
| 62 |
<code>ILoggingEvent</code> interface. Most of the changes required |
| 63 |
in migrating log4j components to logback-classic are related to |
| 64 |
differences in implementation of the <code>LoggingEvent</code> |
| 65 |
class. Rest assured, these differences are rather limited. If in |
| 66 |
spite of your best efforts you are unable to migrate any given |
| 67 |
log4j component to logback-classic, do post a question on the <a |
| 68 |
href="../mailinglist.html">logback-dev mailing list</a>. A logback |
| 69 |
developer should be able to provide guidance. |
| 70 |
</p> |
| 71 |
|
| 72 |
|
| 73 |
<h3 class="doAnchor" name="log4jLayout">Migrating a log4j layout</h3> |
| 74 |
|
| 75 |
<p>Let us begin by migrating a hypothetical and trivially simple |
| 76 |
log4j layout named <a |
| 77 |
href="../xref/chapters/migrationFromLog4j/TrivialLog4jLayout.html">TrivialLog4jLayout</a> |
| 78 |
which returns the message contained in a logging events as the |
| 79 |
formatted message. Here is the code. |
| 80 |
</p> |
| 81 |
|
| 82 |
|
| 83 |
<pre class="prettyprint source">package chapters.migrationFromLog4j; |
| 84 |
|
| 85 |
import org.apache.log4j.Layout; |
| 86 |
import org.apache.log4j.spi.LoggingEvent; |
| 87 |
|
| 88 |
public class TrivialLog4jLayout extends Layout { |
| 89 |
|
| 90 |
public void activateOptions() { |
| 91 |
// there are no options to activate |
| 92 |
} |
| 93 |
|
| 94 |
public String format(LoggingEvent loggingEvent) { |
| 95 |
return loggingEvent.getRenderedMessage(); |
| 96 |
} |
| 97 |
|
| 98 |
public boolean ignoresThrowable() { |
| 99 |
return true; |
| 100 |
} |
| 101 |
}</pre> |
| 102 |
|
| 103 |
<p>The logback-classic equivalent named <a |
| 104 |
href="../xref/chapters/migrationFromLog4j/TrivialLogbackLayout.html">TrivialLogbackLayout</a> |
| 105 |
would be </p> |
| 106 |
|
| 107 |
<pre class="prettyprint source">package chapters.migrationFromLog4j; |
| 108 |
|
| 109 |
import ch.qos.logback.classic.spi.ILoggingEvent; |
| 110 |
import ch.qos.logback.core.LayoutBase; |
| 111 |
|
| 112 |
public class TrivialLogbackLayout extends <b>LayoutBase<ILoggingEvent></b> { |
| 113 |
|
| 114 |
public String <b>doLayout</b>(ILoggingEvent loggingEvent) { |
| 115 |
return loggingEvent.getMessage(); |
| 116 |
} |
| 117 |
} </pre> |
| 118 |
|
| 119 |
<p>As you can see, in a logback-classic layout, the formatting |
| 120 |
method is named <code>doLayout</code> instead of |
| 121 |
<code>format</code>() in log4j. The <code>ignoresThrowable</code>() |
| 122 |
method is not needed and has no equivalent in logback-classic. Note |
| 123 |
that a logback-classic layout must extend the |
| 124 |
<code>LayoutBase<ILoggingEvent></code> class. |
| 125 |
</p> |
| 126 |
|
| 127 |
<p>The <code>activateOptions</code>() method merits further |
| 128 |
discussion. In log4j, a layout will have its |
| 129 |
<code>activateOptions</code>() method invoked by log4j |
| 130 |
configurators, that is <code>PropertyConfigurator</code> or |
| 131 |
<code>DOMConfigurator</code> just after all the options of the |
| 132 |
layout have been set. Thus, the layout will have an opportunity to |
| 133 |
check that its options are coherent and if so, proceed to fully |
| 134 |
initialize itself.</p> |
| 135 |
|
| 136 |
|
| 137 |
<p>In logback-classic, layouts must implement the <a |
| 138 |
href="../xref/ch/qos/logback/core/spi/LifeCycle.html">LifeCycle</a> |
| 139 |
interface which includes a method called <code>start</code>(). The |
| 140 |
<code>start</code>() method is the equivalent of log4j's |
| 141 |
<code>activateOptions</code>() method. |
| 142 |
</p> |
| 143 |
|
| 144 |
<h3 class="doAnchor" name="log4jAppender">Migrating a log4j |
| 145 |
appender</h3> |
| 146 |
|
| 147 |
<p>Migrating an appender is quite similar to migrating a |
| 148 |
layout. Here is a trivially simple appender called <a |
| 149 |
href="../xref/chapters/migrationFromLog4j/TrivialLog4jAppender.html">TrivialLog4jAppender</a> |
| 150 |
which writes on the console the string returned by its layout.</p> |
| 151 |
|
| 152 |
<pre class="prettyprint source">package chapters.migrationFromLog4j; |
| 153 |
|
| 154 |
import org.apache.log4j.AppenderSkeleton; |
| 155 |
import org.apache.log4j.spi.LoggingEvent; |
| 156 |
|
| 157 |
|
| 158 |
public class TrivialLog4jAppender extends AppenderSkeleton { |
| 159 |
|
| 160 |
protected void append(LoggingEvent loggingevent) { |
| 161 |
String s = this.layout.format(loggingevent); |
| 162 |
System.out.println(s); |
| 163 |
} |
| 164 |
|
| 165 |
public void close() { |
| 166 |
// nothing to do |
| 167 |
} |
| 168 |
|
| 169 |
public boolean requiresLayout() { |
| 170 |
return true; |
| 171 |
} |
| 172 |
}</pre> |
| 173 |
|
| 174 |
<p>The logback-classic equivalent named <a |
| 175 |
href="../xref/chapters/migrationFromLog4j/TrivialLogbackAppender.html">TrivialLogbackAppender</a> |
| 176 |
would be written as</p> |
| 177 |
|
| 178 |
|
| 179 |
<pre class="prettyprint source">package chapters.migrationFromLog4j; |
| 180 |
|
| 181 |
import ch.qos.logback.classic.spi.ILoggingEvent; |
| 182 |
import ch.qos.logback.core.AppenderBase; |
| 183 |
|
| 184 |
public class TrivialLogbackAppender extends AppenderBase<ILoggingEvent> { |
| 185 |
|
| 186 |
@Override |
| 187 |
public void start() { |
| 188 |
if (this.layout == null) { |
| 189 |
addError("No layout set for the appender named [" + name + "]."); |
| 190 |
return; |
| 191 |
} |
| 192 |
super.start(); |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
protected void append(ILoggingEvent loggingevent) { |
| 197 |
// note that AppenderBase.doAppend will invoke this method only if |
| 198 |
// this appender was successfully started. |
| 199 |
|
| 200 |
String s = this.layout.doLayout(loggingevent); |
| 201 |
System.out.println(s); |
| 202 |
} |
| 203 |
}</pre> |
| 204 |
|
| 205 |
|
| 206 |
<p>Comparing the two classes, you should notice that the contents |
| 207 |
of the <code>append</code>() method remains unchanged. The |
| 208 |
<code>requiresLayout</code> method is not used in logback and can |
| 209 |
be removed. In logback, the <code>stop</code>() method is the |
| 210 |
equivalent of log4j's <code>close</code>() method. However, |
| 211 |
<code>AppenderBase</code> in logback-classic, contains a nop |
| 212 |
implementation for <code>stop</code> which is sufficient for the |
| 213 |
purposes of this trivial appender. |
| 214 |
</p> |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
<script src="../templates/footer.js" type="text/javascript"></script> |
| 219 |
</div> |
| 220 |
</body> |
| 221 |
</html> |