我对 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();
}
}
任何帮助表示赞赏,谢谢:)