How to print Stack traces into log file, COM interface is calling our jar file, so need to know the stack trace for debugging.

有帮助吗?

解决方案

You can use commons-logging and have code like:

private static final Log LOG_OBJ=LogFactory.getLog(YourClassName.class);

and then:

try {
    //Your implementation here
} catch(Exception e) {
     LOG_OBJ.error("Exception occurred", e);
}

其他提示

What you can do is wrap system error stream and system output stream and redirect it to some file at startup of your application something like this:

 File logFile = new File("MyLog.log");

 PrintStream ps = new PrintStream(new FileOutputStream(logFile));

 System.setOut(ps);
 System.setErr(ps);

This will redirect all your System.out.print and printStackTraces to log file MyLog.log

Or you can implement some logging mechanism using log4j or java logging.

Hope this helps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top