[英]Log4j2 including library name in stacktrace
I just started using of log4j2. 我刚开始使用log4j2。 But i found that log4j2 including library name in stacktrace.
但我发现log4j2包含stacktrace中的库名。 How can i disable that?
我怎么能禁用它?
Here is an example: 这是一个例子:
java.lang.NullPointerException
at com.sev.controllers.UserController.login(UserController.java:35) ~[UserController.class:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_31]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_31]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_31]
at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_31]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
Im talking about that name in [] braces. 我在[]括号中谈论这个名字。
Here is my log4j config 这是我的log4j配置
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.apache.log4j.xml" level="INFO"/>
<Logger name="org.hibernate" level="INFO"/>
<Logger name="org.springframework" level="INFO"/>
<Root level="debug">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
And here is my versions: 这是我的版本:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.3</version>
</dependency>
Forget to mention that my app is spring-boot based. 忘了提一下我的应用程序是基于spring-boot的。
Your configuration's PatternLayout pattern does not contain an explicit exception converter. 您的配置的PatternLayout模式不包含显式异常转换器。 Log4j will provide a default which is %xEx.
Log4j将提供默认值%xEx。 This includes the jar file etc.
这包括jar文件等。
You can change this by explicitly specifying setting the simple %ex converter. 您可以通过明确指定设置简单的%ex转换器来更改此设置。 So your pattern ends in ...%m%ex%n.
因此,您的模式以...%m%ex%n结尾。
You need to set the alwaysWriteExceptions
to false in the pattern layout. 您需要在模式布局
alwaysWriteExceptions
设置为false。
https://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout https://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout
Then add to your pattern the throwable options you would like instead. 然后在您的模式中添加您想要的throwable选项。
https://logging.apache.org/log4j/2.x/manual/layouts.html#Patterns https://logging.apache.org/log4j/2.x/manual/layouts.html#Patterns
Outputs the Throwable trace bound to the LoggingEvent, by default this will output the full trace as one would normally find with a call to Throwable.printStackTrace().
输出绑定到LoggingEvent的Throwable跟踪,默认情况下,这将输出完全跟踪,就像通常调用Throwable.printStackTrace()一样。
You can follow the throwable conversion word with an option in the form %throwable{option}.
您可以使用%throwable {option}形式的选项跟随throwable转换字。
%throwable{short} outputs the first line of the Throwable.
%throwable {short}输出Throwable的第一行。
%throwable{short.className} outputs the name of the class where the exception occurred.
%throwable {short.className}输出发生异常的类的名称。
%throwable{short.methodName} outputs the method name where the exception occurred.
%throwable {short.methodName}输出发生异常的方法名称。
%throwable{short.fileName} outputs the name of the class where the exception occurred.
%throwable {short.fileName}输出发生异常的类的名称。
%throwable{short.lineNumber} outputs the line number where the exception occurred.
%throwable {short.lineNumber}输出发生异常的行号。
%throwable{short.message} outputs the message.
%throwable {short.message}输出消息。
%throwable{short.localizedMessage} outputs the localized message.
%throwable {short.localizedMessage}输出本地化消息。
%throwable{n} outputs the first n lines of the stack trace.
%throwable {n}输出堆栈跟踪的前n行。
Specifying %throwable{none} or %throwable{0} suppresses output of the exception.
指定%throwable {none}或%throwable {0}会抑制异常的输出。
If you still can not satisfy what you want with those options you would need to write a custom converter as described here. 如果您仍然无法满足您的需求,那么您需要编写自定义转换器,如此处所述。
https://logging.apache.org/log4j/2.x/manual/extending.html#PatternConverters https://logging.apache.org/log4j/2.x/manual/extending.html#PatternConverters
An example that is a little more clear then theirs. 一个比他们更清楚的例子。
@Plugin(name="HostNameConverter", category ="Converter")
@ConverterKeys({"h","host","hostName"})
public class HostNameConverter extends LogEventPatternConverter {
/**
* Constructs an instance of LoggingEventPatternConverter.
*
* @param name name of converter.
* @param style CSS style for output.
*/
protected HostNameConverter(String name, String style) {
super(name, style);
}
@Override
public void format(LogEvent event, StringBuilder toAppendTo) {
toAppendTo.append(HostNameUtil.getLocalHostName());
}
public static HostNameConverter newInstance(String[] options){
return new HostNameConverter(HostNameConverter.class.getSimpleName(),HostNameConverter.class.getSimpleName());
}
}
我认为这更好:[ ...%m%n%ex ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.