Java UncaughtExceptionHandler + LOG4J + Maven -
i log file exceptions (shown in console) in maven projects. create class witch implements uncaughtexceptionhandler , call log4j logger.error. works in main maven project (where main class), not inside others maven projects connected (with dependencies in pom.xml). maybe default uncaughtexceptionhandler overridden maven, how can make works?
public static void main(string[] args) throws exception{ thread.setdefaultuncaughtexceptionhandler(new defaultexceptionhandler()); } public class defaultexceptionhandler implements uncaughtexceptionhandler{ private static final logger logger = loggerfactory.getlogger(defaultexceptionhandler.class); @override public void uncaughtexception(thread thread, throwable ex) { logger.error("exception thread: "+thread.getname(), ex); }
}
you can create instance of java.io.printstream
below
public static printstream createloggingproxy( final printstream realprintstream) { return new printstream(realprintstream) { public void print(final string string) { logger.info(string); } public void println(final string string) { logger.warn(string); } }; }
and set appropriate streams this:
system.setout(createloggingproxy(system.out)); system.seterr(createloggingproxy(system.err));
where logger
instance of org.apache.log4j.logger
Comments
Post a Comment