0

我对 Logback 有点问题,我似乎无法理解是什么导致了这个问题。每当我调用 logger.error(String message, Throwable t) 时,堆栈跟踪本身不会被打印,消息会。

输出:([06.02.2021 11:36:52] [Threadpool (Thread 0)] [ERROR]: An error occured while executing a command!下面没有堆栈跟踪)`

我的 logback.xml:

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <target>System.out</target>
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="at.Xirado.Bean.Logging.Layout" />
        </encoder>
    </appender>



    <root level="all">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

我的自定义布局类:

package at.Xirado.Bean.Logging;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.LayoutBase;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Layout extends LayoutBase<ILoggingEvent> {

    public String doLayout(ILoggingEvent event) {
        StringBuffer sbuf = new StringBuffer(128);
        LocalDateTime myDateObj = LocalDateTime.now();
        DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss");
        String formattedDate = myDateObj.format(myFormatObj);
        AttributedStringBuilder asb = new AttributedStringBuilder();
        if(event.getLevel() == Level.ERROR)
        {
            asb.style(AttributedStyle.DEFAULT.foreground(222,23,56));
        }else if(event.getLevel() == Level.WARN)
        {
            asb.style(AttributedStyle.DEFAULT.foreground(255,255,0));
        }
        asb.append("[").append(formattedDate).append("] [")
                .append(event.getThreadName())
                .append("] [")
                .append(event.getLevel().levelStr.toUpperCase())
                .append("]: ")
                .append(event.getFormattedMessage());
        asb.style(AttributedStyle.DEFAULT).append(CoreConstants.LINE_SEPARATOR);
        sbuf.append(asb.toAnsi());
        return sbuf.toString();
    }
}

任何帮助表示赞赏,谢谢:)

4

1 回答 1

0

因为当日志级别为错误时,您的布局返回时没有堆栈跟踪。

    private static String getStackTrace(IThrowableProxy throwable, int maxStackTraceDepth) {
        if(throwable==null) return null;

        StringBuilder stackTraceSB = new StringBuilder(((ThrowableProxy)throwable).getThrowable().toString()+"\n");
        StackTraceElementProxy[] stackTraceElements = throwable.getStackTraceElementProxyArray();
        for (int i = 0; i < stackTraceElements.length; i++) {
            if (i >= maxStackTraceDepth) {
                stackTraceSB.append("\t").append("... " + (stackTraceElements.length - i) + " more");
                break;
            }
            stackTraceSB.append("\tat ").append(stackTraceElements[i].toString()).append("\n");
        }

        return stackTraceSB.toString();
    }

    public String doLayout(ILoggingEvent event) {
        StringBuffer sbuf = new StringBuffer(128);
        //ignore other code

        if(Level.ERROR.equals(event.getLevel())){
            asb.append(getStackTrace(event.getThrowableProxy(),10));
        }

        return sbuf.toString();
    }
于 2021-03-11T06:33:42.360 回答