0

I have some legacy code where there are lot of e.printStackTrace(), those are not printed in log4j file for obvious reasons. The latest code does have log.error("Exception occured in class::method",e);. Is there a standard way of logging those legacy e.printStackTrace() in log4j?

Here is my log4j.properties. I am setting up the property file/log file name within the code

PropertyConfigurator.configure("log4j.properties");
setLog4jFile("log.txt");

private static void setLog4jFile(String logFileName) {
        try {
            Logger rootLogger = Logger.getRootLogger();
            RollingFileAppender appender = new RollingFileAppender(new PatternLayout("%p %t %c - %m%n"), logFileName, false);
            appender.setMaxFileSize("10000KB");
            appender.setMaxBackupIndex(10);
            rootLogger.addAppender(appender);
        }catch (Exception e)    {
            e.printStackTrace();
        }
}

log4j.properties

log4j.rootCategory=info, stdout  
log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F%L) - %m%n  
nilesh
  • 14,131
  • 7
  • 65
  • 79

1 Answers1

2

You can redirect the standard System.err to log4j.

This question and its answers should do the trick:

log4j redirect stdout to DailyRollingFileAppender

If possible though, I would still recommend to rewrite the printStacktrace's into log statements.

Community
  • 1
  • 1
The Nail
  • 8,355
  • 2
  • 35
  • 48