Search code examples
javaexceptionlog4jstack-traceline-breaks

How to print stack traces in separated lines after catching exception in Log4J


Currently, I could get the stack traces:

final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw, true);
throwable.printStackTrace(pw);
String stackTraceAsString = sw.getBuffer().toString();

but when calling log functions like errors(), debug(), etc., I could only get all of the stack traces in 1 line, though the stack trace string contains \r\n. I'd like to separate it into several lines.

Looking into Appender class:

@Override
protected void subAppend(LoggingEvent event) {
    this.qw.write(this.layout.format(event));

    if (this.layout.ignoresThrowable()) {
        String[] s = event.getThrowableStrRep();
        if (s != null) {
            int len = s.length;
            for (int i = 0; i < len; i++) {
                this.qw.write(s[i]);
                this.qw.write(Layout.LINE_SEP);
            }
        }
    }
    if (this.immediateFlush) {
        this.qw.flush();
    }
}

However, it only deals with the exception thrown without being caught. Therefore, I'm implementing a customized appender:

public class CustomRollingFileAppender extends RollingFileAppender {
}

I'm trying to extract string from QuiteWriter variable inside this class, replace '\r\n' it with Layout.LINE_SEP, but I don't know how to extract it.


Solution

  • In short No, there is no way to override a constant interface variable (Layout.LINE_SEP is a constant variable defined in )

    Also, You do not override class variables in Java you hide them. Overriding is for instance methods. Hiding is different from overriding.

    Since, there is no getter or setter method available for Layout.LINE_SEP, we cannot override too.

    Regarding your question, you don't need to print the stack trace to String and then log it. LOG4J provide methods which will print the stack trace for you.

    For example.

    public void log(Priority priority, Object message, Throwable t)
    
    public void error(Object message, Throwable t)
    

    Passing the exception object to the variable t of type throwable will print the stack trace along with line breaks.