SLF4J Migrator
The SLF4J migrator is a small Java tool for migrating Java source
files from the Jakarta Commons Logging (JCL) API to SLF4J. It can
also migrate from the log4j API to SLF4J, or from
java.util.logging
API to SLF4J.
The SLF4J migrator consists of a single jar file that can be launched as a stand-alone java application. Here is the command:
java -jar slf4j-migrator-${version}.jar
Once the application is launched, a window similar to the following should appear.
Use of the application should be self-explanatory. Please note that this migration tool does in-place replacement of Java files, meaning that there will be no back-up copies of modified files. It is your responsibility to backup your files before using SLF4J Migrator.
Limitations
SLF4J migrator is intended as a simple tool to help you to migrate your project source using JCL, log4j or JUL to SLF4J. It can only perform elementary conversion steps. Essentially, it will replace appropriate import lines and logger declarations.
MyClass is a sample class using JCL. Here it is before:
package some.package; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public MyClass { Log logger = LogFactory.getLog(MyClass.class); public void someMethod() { logger.info("Hello world"); } }
and after migration:
package some.package; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public MyClass { Logger logger = LoggerFactory.getLogger(MyClass.class); public void someMethod() { logger.info("Hello world"); } }
Although its conversion rules are elementary, the SLF4J migrator can still alleviate much of the grunt-work involved in migrating a Java project from JCL to SLF4J.
Migration rules from log4j to SLF4J, or from JUL to SLF4J are similar.
General limitations
- Build scripts are not modified
Your Ant/Maven/Ivy build scripts need to be modified manually to use SLF4J instead of JCL or log4j.
- only messages of type String are supported
If one of your log statements contains a non-string object as its sole parameter, you will have to manually add a toString() method call on the object.
For example,
logger.debug(new Object());
has to be manually re-written as
logger.debug(new Object().toString());
- the FATAL level is not supported.
You have to convert them manually. This limitation is not deemed very serious because there are usually very few log statements bearing the FATAL level.
- if a method declares multiple loggers on the same line, the
conversion will not be complete. Example:
public void someMethod(Log l1, Log l2) { ... } will be converted as public void someMethod(Log l1, Logger l2) { ... }
Limitations when migrating from log4j
- NDC statements are left as-is
Since NDC is not supported by SLF4J, the migrator cannot properly handle NDC statements. You have to migrate them to MDC manually. Again, this limitation is not deemed serious because there are usually very few NDC statements even in large projects.
Please note that contrary to NDC, MDC statements are migrated correctly because SLF4J supports such statements.
- Calls to
PropertyConfigurator
orDomConfigurator
cannot be migrated since they have no SLF4J equivalents.
Limitations when migrating from JUL
- Calls to
finest()
,finer()
orfinest()
methods ofjava.util.logging.Logger
are left as is.Given that
finest()
,finer()
orfinest()
calls could map to both trace() or debug() calls in SLF4J, it is impossible to guess how the user wants to map these calls. - All strings matching ".severe(" are replaced by the string
".error(" without any contextual analysis. Similarly, all strings
matching ".warning(" are replaced by ".warn(".
Since the match/replace operation is not contextual, if your code contains methods named "severe" or "warning", then the migration results will have compilation errors. Fortunately, such errors should be rare and easy to identify.
- Invocations of the following methods defined in the
java.util.logging.Logger
class need to be migrated manually:log
,logp
,logrb
,entering
,exiting
.