简体   繁体   English

在子类化Log4j ConsoleAppender时如何避免在字符串内使用\\ n

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

I have written a Log4j Custom Appender based on log4j's ConsoleAppender . 我已经基于log4j的ConsoleAppender编写了Log4j自定义Appender。

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 我正在基于获取的异常stacktrace构造一个String,我面临的问题是我在其中看到字符'\\ n'

Please see the below String i got which has characters like \\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

This is my CustomAppender class 这是我的CustomAppender

please let me know why i am getting \\n inside my String . 请让我知道为什么\\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;
    }

}

I highly doubt the newlines originate from anywhere else but the stringBuffer.append(this.layout.format(event)); 我高度怀疑换行符是否来自stringBuffer.append(this.layout.format(event));其他地方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. “不应该”的意思是,任何Throwable子类都可以重写toString()方法以便在末尾附加\\ n,但是通常不这样做,而org.springframework.remoting.RemoteLookupFailureException则不这样。

What Layout implementation does your class get initialized with? 您的班级使用哪种布局实现初始化? And how is that layout's format() method implemented? 以及该布局的format()方法如何实现? Hopefully your IDE supports conditional breakpoints and you can stop the debugger after an append if stringBuffer.toString().contains("\\n"); 希望您的IDE支持条件断点,并且如果stringBuffer.toString().contains("\\n");可以在追加后停止调试器stringBuffer.toString().contains("\\n");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM