log4j not printing the stacktrace for exceptions

后端 未结 8 1652
清酒与你
清酒与你 2020-12-04 13:54

I am using log4j with tomcat. When I log exceptions in my JSPs, servlets:

private Logger _log = Logger.getLogger(this.getClass());
...
try{...} catch (Except         


        
相关标签:
8条回答
  • 2020-12-04 14:50

    There are two overloaded methods for error method.

    1. logger.error(ex);
    2. logger.error("some oops string ", ex);

    if you use 1st method , which will only print the name of the Exception. if you use 2nd method, some message along with exception which will print complete stack trace similar to e.printStackTrace() method.

    0 讨论(0)
  • 2020-12-04 14:51

    I haven't used the fillStackTrace call, so I cannot comment if that will work. Another approach is to use a little method that returns the formatted text from an Exception.

    public static String getStackTrace(Exception e)
    {
        StringWriter sWriter = new StringWriter();
        PrintWriter pWriter = new PrintWriter(sWriter);
        e.printStackTrace(pWriter);
        return sWriter.toString();
    }
    

    In your logging code, you could write:

    logger.error("An exception occurred: " + Utils.getStackTrace(e));
    
    0 讨论(0)
提交回复
热议问题