Question

I have written a Log4j Custom Appender based on log4j's ConsoleAppender.

I am constructing a String based on the exception stacktrace i get , the problem i am facing is that i am seeing charcters '\n' inside it

Please see the below String i got which has characters like \n .

(Test.java:<sendSymbol>:40)- THE ERROR IS \norg.springframework.remoting.RemoteLookupFailureException: Lookup of RMI stub failed; nested exception is java.rmi.ConnectException: Connection refused to host: 19.9.199.155; nested exception is: \n

This is my CustomAppender class

please let me know why i am getting \n inside my String .

package com;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.ThrowableInformation;

public class TestAppender extends AppenderSkeleton {
    public TestAppender() {



    }
    public TestAppender(Layout layout) {
        this.layout = layout;
    }
    public void append(LoggingEvent event) {

        ThrowableInformation throwableInfo = null;
        Throwable throwable = null;
        Throwable cause = null;
        StackTraceElement[] stackTrace = null;
        String smallIndent = "";
        String largeIndent = " at ";
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(this.layout.format(event));
        if ((throwableInfo = event.getThrowableInformation()) != null) {
            if ((throwable = throwableInfo.getThrowable()) != null) {
                cause = throwable;
                while (cause != null) {
                    stringBuffer.append(smallIndent);
                    if (cause != throwable) {
                        stringBuffer.append("Caused by: ");
                    }
                    stringBuffer.append(cause.toString());
                    stackTrace = cause.getStackTrace();
                    for (int i = 0; i < stackTrace.length; i++) {
                        stringBuffer.append(largeIndent + stackTrace[i]);
                    }

                    cause = cause.getCause();
                }
            }
        }


        System.out.println(stringBuffer.toString());

    }

    public boolean requiresLayout() {
        return true;
    }

    public void close() {
        if (this.closed) {
            return;
        }
        this.closed = true;
    }

}
Était-ce utile?

La solution

I highly doubt the newlines originate from anywhere else but the stringBuffer.append(this.layout.format(event)); call. The other four calls should not add any new lines. From your output example :

stringBuffer.append(largeIndent + stackTrace[i]); // -- is never called
stringBuffer.append(smallIndent); // -- won't add newline
stringBuffer.append("Caused by: "); // -- won't add newline (and not called)
stringBuffer.append(cause.toString()); // -- shouldn't add newline

By "shouldn't" I mean, any Throwable subclass can override the toString() method in order to append a \n at the end, but they generally don't and org.springframework.remoting.RemoteLookupFailureException does not.

What Layout implementation does your class get initialized with? And how is that layout's format() method implemented? Hopefully your IDE supports conditional breakpoints and you can stop the debugger after an append if stringBuffer.toString().contains("\n");

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top