Java – convert e.printstacktrace() to use log4j instead

I'm new to log4j I don't want to display an exception stack trace in my log file, for example

java.lang.IllegalArgumentException: nodeRef is a mandatory parameter
at org.alfresco.util.ParameterCheck.mandatory(ParameterCheck.java:42)

These exceptions are written directly to the console by using e.printstacktrace()

try {
    // something
} catch(Exception e) {
    StringWriter stack = new StringWriter();
    e.printStackTrace(new PrintWriter(stack));
    logger.debug("Caught exception; decorating with appropriate status template : " + stack.toString());
}

I am now customizing an open source project instead of writing all my own programs@ H_ 403_ 9@ so it is impossible to delete e.printstacktrace(); In all java files

As far as I know, log4j logs printed with logger can be configured by using log4j error level, such as info, debug, warn How does but write directly to the console or file?

How to configure log4j not to print only exception stack traces but to display other information? Is there a solution?

Solution

code

StringWriter stack = new StringWriter();
e.printStackTrace(new PrintWriter(stack));
logger.debug("Caught exception; decorating with appropriate status template : " + stack.toString());

This is just an example of log4j API usage errors However, if you insist on using it, you won't need to do anything except raise the log level to debug and omit the entire message

There may be a way to truncate all log messages, which can alleviate your problem to some extent

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>