我写了一个基于 log4j 的 Log4j Custom Appender ConsoleAppender
。
我正在根据我得到的异常堆栈跟踪构造一个字符串,我面临的问题是我在其中看到字符 '\n'
请参阅下面我得到的字符串,其中包含\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
这是我的CustomAppender
课
请让我知道我为什么会得到\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;
}
}