簡體   English   中英

在子類化Log4j ConsoleAppender時如何避免在字符串內使用\\ n

[英]How to avoid \n inside a String while subclassing Log4j ConsoleAppender

我已經基於log4j的ConsoleAppender編寫了Log4j自定義Appender。

我正在基於獲取的異常stacktrace構造一個String,我面臨的問題是我在其中看到字符'\\ 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;
    }

}

我高度懷疑換行符是否來自stringBuffer.append(this.layout.format(event));其他地方stringBuffer.append(this.layout.format(event)); 呼叫。 其他四個調用不應添加任何新行。 從您的輸出示例:

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

“不應該”的意思是,任何Throwable子類都可以重寫toString()方法以便在末尾附加\\ n,但是通常不這樣做,而org.springframework.remoting.RemoteLookupFailureException則不這樣。

您的班級使用哪種布局實現初始化? 以及該布局的format()方法如何實現? 希望您的IDE支持條件斷點,並且如果stringBuffer.toString().contains("\\n");可以在追加后停止調試器stringBuffer.toString().contains("\\n");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM