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