Convert e.printStackTrace () to use log4j instead

I am very new to log4j. I don't want to show the 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 using e.printStackTrace()something like this.

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());
}

Now I am setting up an open source project without writing all my programs myself. Thus, it is impossible to delete e.printStackTrace();in all java files.

As far as I know, a log that is printed log4jusing a log can be configured using the log4j error level, such as information, debug, warn.But, how to write directly to the console or file?

log4j, stacktrace, ? ?

+5
3

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

- API Log4j. , , debug .

, .

+8

Logger #

import org.apache.log4j.Logger;

...

Logger log = Logger.getLogger(Locomotive.class);
log.error("your message", exc);

...
+4
logger.info("Inside add() of Abc class");
logger.error("Error occurred while calculation::::"+errorMessage);
0
source

All Articles