Skip to content

Instantly share code, notes, and snippets.

@cboddy
Created June 30, 2018 09:08
Show Gist options
  • Save cboddy/31874bcdc68d06a514eefa2f2e1b4cec to your computer and use it in GitHub Desktop.
Save cboddy/31874bcdc68d06a514eefa2f2e1b4cec to your computer and use it in GitHub Desktop.
Update a java project that is using System.{out,err} for logging to use java.util.logger.
# usage:
# ./with_logger.sh path_to_project_src
#
for f in `find $1 -type f -name \*java |xargs grep -l System.out`;
do
echo $f
# add import statement after package declaration
sed -i '0,/package.*/s//&\nimport java.util.logging.*;/' $f
# add Logger instance after class declaration
sed -i '0,/.*class [a-zA-Z]\+.*/s//&\n\tprivate static final Logger LOG = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);/' $f
# replace System.out.println with LOG.info
sed -i 's/System.out.println/LOG.info/g' $f
# replace System.err.println wiwth LOG.error
sed -i 's/System.err.println/LOG.severe/g' $f
# replace ex.printStackTrace with LOG.log(Level.WARNNING, ex.getMessage(), ex)
sed -i 's/\([a-zA-Z]\+\).printStackTrace()/LOG.log(Level.WARNING, \1.getMessage(), \1)/' $f
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment