问题描述
我正在尝试让自定义throwableRenderer与log4j 1.2.17一起使用.
请注意,我目前无法升级到log4j2,因此我正在寻找1.x解决方案.
I am trying to get a custom throwableRenderer to work with log4j 1.2.17.
Note that I cannot upgrade to log4j2 at this stage so I am looking for 1.x solution.
例如参见
如何制作log4j syslog追加器在一行中写一个堆栈跟踪?
See e.g.
How to make log4j syslog appender write a stack trace in one line?
我正试图做到这一点-获取要在1行上打印的堆栈跟踪.我尝试了两种可以在网上找到的方法-使用自定义渲染器和使用增强模式布局.还是没有运气!
I am trying to do just that - get the stack trace to be printed on 1 line. I tried 2 approaches I could find on the web - using custom renderer and using Enhanced Pattern Layout. Still no luck!
但是类WRThrowableRenderer
(这是我的自定义渲染器)
它的方法doRender
根本没有被调用.
这一切都在WildFly 8(Java 8)内运行的Web应用程序中实现.
But the class WRThrowableRenderer
(which is my custom renderer)
and its method doRender
is simply not called.
This is all in a web app running inside WildFly 8 (Java 8).
在测试这两种方法时,我尝试了至少10种不同的方法,但没有任何效果.
I tried at least 10 different things while testing the two approaches but nothing works.
我在做什么错了?!
此外,该渲染器是否应该影响所有记录器并在记录异常时更改其行为?我想是这样.我问这个问题是因为我在rootLogger下有子记录器.它们都通过rootLogger记录在一个文件中.
Also, is this renderer supposed to affect all loggers and change their behavior when an exception is logged? I think so. I am asking this because I have child loggers under this rootLogger. And they all log via the rootLogger in one single file.
log4j.rootLogger=INFO, stdout
log4j.throwableRenderer=com.yb.common.logging.WRThrowableRenderer
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
# log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%t] ###%c{20}:%L### - [[[%m]]]%n
# log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%t] ###%c{20}### [[[%m]]]%n %throwable{separator(|)}
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%t] ###%c{20}### [[[%m]]]%n
# log4j.appender.stdout.layout.ConversionPattern=%m%n
log4j.appender.stdout.threshold=INFO
log4j.appender.stdout.immediateFlush=true
推荐答案
没有自定义的ThrowableRenderer
如果您希望不使用自定义ThrowableRenderer
而将堆栈跟踪全部记录在一行上,那么您最好的办法就是获取堆栈跟踪的第一行.
Without a custom ThrowableRenderer
If you want the stack trace all on one line without using a custom ThrowableRenderer
, the best you'll be able to do is get the first line of the stack trace.
例如,使用以下配置:
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d] %-5p %m %throwable{short}%n
将生成此日志:
[2020-11-20 10:54:53,454] ERROR Test error message, with stack trace java.lang.IllegalArgumentException: Test exception message
使用自定义的ThrowableRenderer
如果要将整个堆栈跟踪打印在一行上,则需要使用自定义的ThrowableRenderer.
With a custom ThrowableRenderer
If you want the entire stack trace printed on one line, you'll need to use a custom ThrowableRenderer.
-
创建自定义的ThrowableRenderer,例如
Create the custom ThrowableRenderer, e.g.
package org.example;
import org.apache.log4j.DefaultThrowableRenderer;
import org.apache.log4j.spi.ThrowableRenderer;
import java.util.ArrayList;
import java.util.Arrays;
public class CustomThrowableRenderer implements ThrowableRenderer {
private final DefaultThrowableRenderer defaultRenderer = new DefaultThrowableRenderer();
@Override
public String[] doRender(Throwable throwable) {
String[] defaultRepresentation = defaultRenderer.doRender(throwable);
String[] newRepresentation = {String.join("|", Arrays.asList(defaultRepresentation))};
return newRepresentation;
}
}
配置log4j1以使用自定义的ThrowableRenderer
Configure log4j1 to use the custom ThrowableRenderer
log4j.throwableRenderer=org.example.CustomThrowableRenderer
这时,日志的堆栈跟踪部分将全部放在一行上,尽管它可能与日志的其余部分不在同一行上.
At this point, the stack trace portion of the log will all be on one line, although it may be on a separate line from the rest of the log.
例如,上面的配置:
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%t] ###%c{20}### [[[%m]]]%n
log4j.throwableRenderer=org.example.CustomThrowableRenderer
将生成两行,因为默认情况下堆栈跟踪位于其自己的行上:
Will generate two lines because the stack trace is put on its own line by default:
2020-11-20 11:45:04.706 ERROR [main] ###org.example.App### [[[Test error message, with stack trace]]]
java.lang.IllegalArgumentException: Test exception message| at org.example.App.logErrorWithStackTrace(App.java:31)| at org.example.App.okayThatsEnough(App.java:25)| at org.example.App.notLongEnough(App.java:21)| at org.example.App.makeStackTraceLonger(App.java:17)| at org.example.App.testLoggingWithStackTraces(App.java:13)| at org.example.App.main(App.java:9)
通过在模式中使用%throwable
,您可以在一行上获取错误消息和堆栈跟踪:
You can get the error message and stack trace on one line by using %throwable
in your pattern:
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%t] ###%c{20}### [[[%m]]] %throwable%n
但是在以下情况下将生成空白行
However a blank line will be generated after:
2020-11-20 11:46:46.897 ERROR [main] ###org.example.App### [[[Test error message, with stack trace]]] java.lang.IllegalArgumentException: Test exception message| at org.example.App.logErrorWithStackTrace(App.java:31)| at org.example.App.okayThatsEnough(App.java:25)| at org.example.App.notLongEnough(App.java:21)| at org.example.App.makeStackTraceLonger(App.java:17)| at org.example.App.testLoggingWithStackTraces(App.java:13)| at org.example.App.main(App.java:9)
这可能也可以解决,但可能需要自定义附加程序.
That could probably be fixed as well but it might require a custom appender.
我制作了一个小的示例应用程序,您可以将其用作参考: https://github.com/bmaupin/junkpile/tree/master/java/log4j1-custom-throwablerenderer
I made a small sample app you can use as a reference: https://github.com/bmaupin/junkpile/tree/master/java/log4j1-custom-throwablerenderer
这篇关于自定义ThrowableRenderer无法正常工作log4j 1.x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!