Friday, November 19, 2010

How to change the logging facility in java from org.apache.commons.logging.Log to java.util.logging.Logger;

With a shellscript and some Perl of course!


#!/bin/sh

#
# Note 1: make a backup.
# Note 2: check result and edit manually
#

find . -name \*.java -exec perl -pi -e's/([\s\t](?:(?:private|static|final)[\s\t]+)*)Log(\s+\w+\s+=\s+)LogFactory.getLog\(\s*([^.]+)\.class\s*\)/${1}Logger${2}Logger.getLogger($3.class.getName())/' \{\} \;

find . -name \*.java -exec perl -pi -e's/(import\s+)org.apache.commons.logging.Log\s*;\s*(\r?\n)/import java.util.logging.Logger;$2import java.util.logging.Level;$2/' \{\} \;
find . -name \*.java -exec perl -pi -e's/import\s+org.apache.commons.logging.LogFactory\s*;\s*\r?\n//' \{\} \;

#isWarnEnabled
#isInfoEnabled
#isDebugEnabled
#isTraceEnabled

find . -name \*.java -exec perl -pi -e's/log.isWarnEnabled\(\)/log.isLoggable(Level.WARNING)/g' \{\} \;
find . -name \*.java -exec perl -pi -e's/log.isInfoEnabled\(\)/log.isLoggable(Level.INFO)/g' \{\} \;
find . -name \*.java -exec perl -pi -e's/log.isDebugEnabled\(\)/log.isLoggable(Level.FINE)/g' \{\} \;
find . -name \*.java -exec perl -pi -e's/log.isTraceEnabled\(\)/log.isLoggable(Level.FINER)/g' \{\} \;

#fatal
#error
#warn
#info
#debug
#trace

find . -name \*.java -exec perl -pi -e's/log.fatal\(/log.log(Level.SEVERE, /g' \{\} \;
find . -name \*.java -exec perl -pi -e's/log.error\(/log.log(Level.WARNING, /g' \{\} \;
find . -name \*.java -exec perl -pi -e's/log.warn\(/log.log(Level.WARNING, /g' \{\} \;
find . -name \*.java -exec perl -pi -e's/log.info\(/log.log(Level.INFO, /g' \{\} \;
find . -name \*.java -exec perl -pi -e's/log.debug\(/log.log(Level.FINE, /g' \{\} \;
find . -name \*.java -exec perl -pi -e's/log.trace\(/log.log(Level.FINER, /g' \{\} \;

No comments:

Post a Comment