This is a Python script which allows you to migrate Jakarta Commons Logging & Log4j in your Java source code to SLF4J Logging.
Code has been updated to be compatible with Python 3.x
1. "import" clause will be replaced with SLF4J
-import org.apache.log4j.Logger;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
2. log instance will be created with SLF4J
-private final static Logger log = Logger.getLogger(UserSessionFilter.class);
+private final static Logger log = LoggerFactory.getLogger(UserSessionFilter.class);
3. log clause will be standard SLF4J placeholder based instead of string conjunction
-log.info("UserName="+username+" had login attempted within " + delaySeconds + " seconds by same IP="+ipAddress+" address combination");
+log.info("UserName={} had login attempted within {} seconds by same IP={} address combination", username, delaySeconds, ipAddress);
4. Remove LogSF in Log4j
-LogSF.debug(log, "Input displayFileName={} originalFileName={}", uiTicTemp.getDisplayName(), uiTicTemp.getOriginalFileName());
+log.debug("Input displayFileName={} originalFileName={}", uiTicTemp.getDisplayName(), uiTicTemp.getOriginalFileName());
python refine_logging.py
This will make the changes over all the sample java files in the current working folder. You could diff the changes by running
git diff
If you want to revert the java changes, running:
git reset --hard
cp refine_logging.py YOUR_JAVA_SRC_PATH
python refine_logging.py
The migrator will recursively scan and change your java files under YOUR_JAVA_SRC_PATH, take attention on the output of the script, it will prompt issues/infos if any.