java Logback - 过滤堆栈跟踪
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11559477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Logback - filtering stack trace
提问by viniolli
I'm using logback for logging and i found some problem with filtering stack trace. I hava a structure like this:
我正在使用 logback 进行日志记录,但发现过滤堆栈跟踪存在一些问题。我有这样的结构:
public class Main {
static final Logger logger = (Logger) LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
logger.debug("Start");
MyObject1 loggingElement = new MyObject1();
loggingElement.logg();
logger.debug("End");
}
}
public class MyObject1 {
public MyObject2 obj;
static final Logger logger = (Logger) LoggerFactory.getLogger(MyObject1.class);
public MyObject1() {
obj = new MyObject2();
}
public void logg() {
obj.loggError();
}
}
public class MyObject2 {
static final Logger logger = (Logger) LoggerFactory.getLogger(MyObject2.class);
public void loggError() {
logger.error("Error info", new Throwable("Error"));
}
}
and configuration xml like this:
和配置xml是这样的:
<property name="mask"
value="MyObject2"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss} | %level | %-4thread | %-21logger | %m%n
</pattern>
</encoder>
</appender>
<root level="${root.level:-TRACE}">
<appender-ref ref="STDOUT" />
</root>
When i run the main method i get output like this:
当我运行 main 方法时,我得到如下输出:
12:29:35 | DEBUG | main | com.logging.Main | Start
12:29:35 | ERROR | main | com.logging.MyObject2 | Error info
java.lang.Throwable: Error
at com.logging.MyObject2.loggError(MyObject2.java:11) [bin/:na]
at com.logging.MyObject1.logg(MyObject1.java:17) [bin/:na]
at com.logging.Main.main(Main.java:14) [bin/:na]
12:29:35 | DEBUG | main | com.logging.Main | End
i would like to remove line with 'MyObject1' in it and leave rest of stack trace untouched just like int this example: http://java.dzone.com/articles/filtering-stack-trace-hell
我想删除其中包含“MyObject1”的行,并像本例中的 int 一样保留其余的堆栈跟踪:http: //java.dzone.com/articles/filtering-stack-trace-hell
i tried to change configuration of logback
我试图改变 logback 的配置
<pattern>%d{HH:mm:ss} | %level | %-4thread | %-21logger | %m%n%ex{full,${mask}}
</pattern>
doesn't work at all
根本不起作用
<pattern>%d{HH:mm:ss} | %level | %-4thread | %-21logger | %m%n%eXe{full,${mask}}
</pattern>
removes whole stack trace(not solution for me)
删除整个堆栈跟踪(不是我的解决方案)
Anyone knows anything that could help?
任何人都知道有什么可以帮助的吗?
回答by E-Riz
This capability has been included in Logback Classic as of version 1.1.3. It seems to apply to the following Conversion Words of PatternLayout:
从版本 1.1.3开始,此功能已包含在 Logback Classic 中。它似乎适用于PatternLayout的以下转换词:
回答by Sylhare
On my case I used a ThrowableConverter
for the stacktrace.
在我的情况下,我使用 aThrowableConverter
作为堆栈跟踪。
You can add that to your logback.xml where you have your consoleApender, inside your encoder:
您可以将它添加到您的 logback.xml 中,在您的编码器内有您的 consoleAppender:
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<!-- your other encoder configurations -->
<stackTrace>
<fieldName>stacktrace</fieldName>
<throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
<pattern>[%thread] - %msg%n%stack{1,1024,10,rootFirst}</pattern>
</throwableConverter>
</stackTrace>
</encoder>
</appender>
It will add a field name "stack_trace"with the stack trace. And it does the job for me.
它将在堆栈跟踪中添加一个字段名称“stack_trace”。它为我完成了这项工作。
Inside stack you have the parameters:
在堆栈内部,您有参数:
- maxDepthPerThrowable: Limits the number of stackTraceElements per throwable
- maxLength: Limits the total length in characters of the trace.
- shortenedClassNameLength: Abbreviates class names based on the
shortenedClassName
- rootCauseFirst: Outputs in either 'normal' order (root-cause-last), or root-cause-first
- maxDepthPerThrowable:限制每个 throwable 的 stackTraceElements 数量
- maxLength:限制跟踪字符的总长度。
- shortedClassNameLength:基于类名的缩写
shortenedClassName
- rootCauseFirst:以“正常”顺序(root-cause-last)或 root-cause-first 输出