• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>SLF4J Migrator</title>
8    <link rel="stylesheet" type="text/css" media="screen" href="css/site.css" />
9  </head>
10  <body>
11    <script type="text/javascript">prefix='';</script>
12
13    <script src="templates/header.js" type="text/javascript"></script>
14    <div id="left">
15      <script src="templates/left.js" type="text/javascript"></script>
16    </div>
17    <div id="content">
18
19
20    <h1>SLF4J Migrator</h1>
21
22    <p>The SLF4J migrator is a small Java tool for migrating Java source
23    files from the Jakarta Commons Logging (JCL) API to SLF4J. It can
24    also migrate from the log4j API to SLF4J, or from
25    <code>java.util.logging</code> API to SLF4J.
26    </p>
27
28    <p>The SLF4J migrator consists of a single jar file that can be
29    launched as a stand-alone java application. Here is the command:
30    </p>
31
32    <p class="source">java -jar slf4j-migrator-${version}.jar </p>
33
34    <br/>
35
36    <p>Once the application is launched, a window similar to the
37    following should appear.
38    </p>
39
40    <p><img src="images/slf4j-migrator.gif" alt="slf4j-migrator.gif"/></p>
41
42    <p>Use of the application should be self-explanatory. Please note that
43    this migration tool does in-place replacement of Java files, meaning
44    that there will be no back-up copies of modified files. <b>It is
45    your responsibility to backup your files before using SLF4J
46    Migrator.</b>
47    </p>
48
49
50    <h2>Limitations</h2>
51
52    <p>SLF4J migrator is intended as a simple tool to help you to
53    migrate your project source using JCL, log4j or JUL to SLF4J. It can
54    only perform elementary conversion steps. Essentially, it will
55    replace appropriate import lines and logger declarations.
56    </p>
57
58    <p>MyClass is a sample class using JCL. Here it is before:</p>
59
60    <p class="source">package some.package;
61
62<b>import org.apache.commons.logging.Log;
63import org.apache.commons.logging.LogFactory;</b>
64
65public MyClass {
66
67  <b>Log logger = LogFactory.getLog(MyClass.class);</b>
68
69  public void someMethod() {
70    logger.info("Hello world");
71  }
72}</p>
73
74  <p>and after migration:</p>
75
76  <p class="source">package some.package;
77
78<b>import org.slf4j.Logger;
79import org.slf4j.LoggerFactory;</b>
80
81public MyClass {
82
83  <b>Logger logger = LoggerFactory.getLogger(MyClass.class);</b>
84
85  public void someMethod() {
86    logger.info("Hello world");
87  }
88}</p>
89
90  <br/>
91
92  <p>Although its conversion rules are elementary, the SLF4J migrator
93  can still alleviate much of the grunt-work involved in migrating a
94  Java project from JCL to SLF4J.
95  </p>
96
97  <p>Migration rules from log4j to SLF4J, or from JUL to SLF4J are
98  similar.</p>
99
100  <h3>General limitations</h3>
101
102  <ul>
103
104    <li>Build scripts are not modified
105
106    <p>Your Ant/Maven/Ivy build scripts need to be modified manually to
107    use SLF4J instead of JCL or log4j.</p>
108
109    <p></p>
110    </li>
111
112    <li>only messages of type String are supported
113
114    <p>If one of your log statements contains a non-string object as
115    its sole parameter, you will have to manually add a toString()
116    method call on the object.
117    </p>
118
119    <p>For example,</p>
120    <p class="source">logger.debug(new Object()); </p>
121    <p>has to be manually re-written as</p>
122    <p class="source">logger.debug(new Object().toString()); </p>
123
124    <p></p>
125    </li>
126
127    <li>the FATAL level is not supported.
128
129    <p>You have to convert them manually. This limitation is not
130    deemed very serious because there are usually very few log
131    statements bearing the FATAL level.
132    </p>
133
134    <p>
135    </p>
136    </li>
137
138    <li>if a method declares multiple loggers on the same line, the
139    conversion will not be complete. Example:
140
141    <p class="source">  public void someMethod(Log l1, Log l2) {
142   ...
143  }
144
145will be converted as
146
147  public void someMethod(Log l1, Logger l2) {
148   ...
149  } </p>
150  </li>
151  </ul>
152
153  <h3>Limitations when migrating from log4j</h3>
154
155  <ul>
156    <li>NDC statements are left as-is
157
158    <p>Since NDC is not supported by SLF4J, the migrator cannot
159    properly handle NDC statements. You have to migrate them to MDC
160    manually. Again, this limitation is not deemed serious because
161    there are usually very few NDC statements even in large projects.
162    </p>
163
164    <p>Please note that contrary to NDC, MDC statements are migrated
165    correctly because SLF4J supports such statements.</p>
166
167    <p></p>
168    </li>
169
170    <li>Calls to <code>PropertyConfigurator</code> or
171    <code>DomConfigurator</code> cannot be migrated since they have no
172    SLF4J equivalents.
173
174    <p>
175    </p>
176
177    </li>
178  </ul>
179
180  <h3>Limitations when migrating from JUL</h3>
181
182
183  <ul>
184    <li>Calls to <code>finest()</code>, <code>finer()</code> or
185    <code>finest()</code> methods of
186    <code>java.util.logging.Logger</code> are left as is.
187
188    <p>Given that <code>finest()</code>, <code>finer()</code> or
189    <code>finest()</code> calls could map to both trace() or debug()
190    calls in SLF4J, it is impossible to guess how the user wants to
191    map these calls.
192    </p>
193
194    <p>
195    </p>
196
197    </li>
198
199
200    <li>All strings matching ".severe(" are replaced by the string
201    ".error(" without any contextual analysis. Similarly, all strings
202    matching ".warning(" are replaced by ".warn(".
203
204    <p>Since the match/replace operation is not contextual, if your
205    code contains methods named "severe" or "warning", then the
206    migration results will have compilation errors. Fortunately, such
207    errors should be rare and easy to identify.
208    </p>
209
210    <p>
211    </p>
212
213    </li>
214
215    <li>Invocations of the following methods defined in the
216    <code>java.util.logging.Logger</code> class need to be migrated
217    manually: <code>log</code>, <code>logp</code>, <code>logrb</code>,
218    <code>entering</code>, <code>exiting</code>.
219
220    </li>
221  </ul>
222
223  <script  src="templates/footer.js" type="text/javascript"></script>
224 </div>
225</body>
226</html>
227