如何覆盖 Log4j 2 套接字附加程序中的 DefaultErrorHandler?
How do I override DefaultErrorHandler in a Log4j 2 Socket appender?
目前我在 XML 中定义的 <Socket>
appender 使用 org.apache.logging.log4j.core.appender.DefaultErrorHandler
但它打印了一些我不想出现在 catalina.out
中的信息。
log4j2.xml
这是我目前拥有的:
<Appenders>
<Socket name="socketLoggerAdapter" host="my-server" port="4006" protocol="UDP" reconnectionDelayMillis="60000">
<JsonLayout properties="true"/>
</Socket>
...
<Appenders>
我希望我可以添加一个子元素 <handler class="com.mycompany.MyErrorHandler">
但我在 Log4j 2 文档中找不到任何解决这个用例的内容。
有人可以指点我一些文档或提供有关如何实现它的基本步骤吗?
我要压抑的东西
我的 catalina.out
在 tomcat 启动期间偶尔会记录这个,我不希望它被报告:
2020-06-30 18:12:38,238 AsyncAppender-myAppenderRef-socketAppender ERROR An exception occurred processing Appender socketAppender org.apache.logging.log4j.core.appender.AppenderLoggingException: Error flushing stream UDP:myserver:9006
at org.apache.logging.log4j.core.appender.OutputStreamManager.flush(OutputStreamManager.java:176)
at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:107)
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:165)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:138)
at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:129)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:97)
at com.mycompany.MyCustomAppender$AsyncThread.callAppender(MyCustomAppender.java:455)
at com.mycompany.MyCustomAppender$AsyncThread.run(MyCustomAppender.java:398)
Caused by: java.io.IOException: Message too long
... 8 more
其他信息
错误处理程序的初始化方式如下:
org.apache.logging.log4j.core.appender.DefaultErrorHandler.<init>(org.apache.logging.log4j.core.Appender) line: 47
org.apache.logging.log4j.core.appender.ConsoleAppender(org.apache.logging.log4j.core.appender.AbstractAppender).<init>(java.lang.String, org.apache.logging.log4j.core.Filter, org.apache.logging.log4j.core.Layout<? extends java.io.Serializable>, boolean) line: 40
org.apache.logging.log4j.core.appender.ConsoleAppender(org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender<M>).<init>(java.lang.String, org.apache.logging.log4j.core.Layout<? extends java.io.Serializable>, org.apache.logging.log4j.core.Filter, boolean, boolean, M) line: 60
org.apache.logging.log4j.core.appender.ConsoleAppender.<init>(java.lang.String, org.apache.logging.log4j.core.Layout<? extends java.io.Serializable>, org.apache.logging.log4j.core.Filter, org.apache.logging.log4j.core.appender.OutputStreamManager, boolean) line: 69
org.apache.logging.log4j.core.appender.ConsoleAppender.createDefaultAppenderForLayout(org.apache.logging.log4j.core.Layout<? extends java.io.Serializable>) line: 106
org.apache.logging.log4j.core.config.DefaultConfiguration.<init>() line: 62
org.apache.logging.log4j.core.LoggerContext.<init>(java.lang.String, java.lang.Object, java.net.URI) line: 75
org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.locateContext(java.lang.ClassLoader, java.net.URI) line: 145
org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(java.lang.String, java.lang.ClassLoader, boolean, java.net.URI) line: 70
org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(java.lang.String, java.lang.ClassLoader, boolean) line: 57
org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(java.lang.String, java.lang.ClassLoader, java.lang.Object, boolean) line: 141
org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(java.lang.String, java.lang.ClassLoader, java.lang.Object, boolean) line: 41
org.apache.logging.log4j.LogManager.getContext(java.lang.ClassLoader, boolean) line: 185
org.apache.logging.slf4j.Log4jLoggerFactory(org.apache.logging.log4j.spi.AbstractLoggerAdapter<L>).getContext(java.lang.Class<?>) line: 103
org.apache.logging.slf4j.Log4jLoggerFactory.getContext() line: 43
org.apache.logging.slf4j.Log4jLoggerFactory(org.apache.logging.log4j.spi.AbstractLoggerAdapter<L>).getLogger(java.lang.String) line: 42
org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(java.lang.String) line: 29
org.slf4j.LoggerFactory.getLogger(java.lang.String) line: 358
org.apache.log4j.Logger(org.apache.log4j.Category).<init>(java.lang.String) line: 57
org.apache.log4j.Logger.<init>(java.lang.String) line: 37
org.apache.log4j.Log4jLoggerFactory.getLogger(java.lang.String) line: 43
org.apache.log4j.Logger.getLogger(java.lang.String) line: 41
org.apache.log4j.Logger.getLogger(java.lang.Class) line: 49
com.mycompany.core.security.SessionUtil.<clinit>() line: 24
com.mycompany.MyCompanyContextLoaderListener.contextInitialized(javax.servlet.ServletContextEvent) line: 14
org.apache.catalina.core.StandardContext.listenerStart() line: 4812
org.apache.catalina.core.StandardContext.startInternal() line: 5255
org.apache.catalina.core.StandardContext(org.apache.catalina.util.LifecycleBase).start() line: 150
org.apache.catalina.core.StandardHost(org.apache.catalina.core.ContainerBase).addChildInternal(org.apache.catalina.Container) line: 725
org.apache.catalina.core.StandardHost(org.apache.catalina.core.ContainerBase).addChild(org.apache.catalina.Container) line: 701
org.apache.catalina.core.StandardHost.addChild(org.apache.catalina.Container) line: 717
org.apache.catalina.startup.HostConfig.deployWAR(org.apache.catalina.util.ContextName, java.io.File) line: 945
org.apache.catalina.startup.HostConfig$DeployWar.run() line: 1795
java.util.concurrent.Executors$RunnableAdapter<T>.call() line: 471
java.util.concurrent.FutureTask<V>.run() line: 262
java.util.concurrent.ThreadPoolExecutor.runWorker(java.util.concurrent.ThreadPoolExecutor$Worker) line: 1145
java.util.concurrent.ThreadPoolExecutor$Worker.run() line: 615
java.lang.Thread.run() line: 745
我相信最近还有另一个关于此的 Whosebug 问题。这是我的疏忽,您应该为此创建一个 Jira 问题。虽然您可以调用 setErrorHandler 方法来更改 ErrorHandler,但无法配置一个,因为它未初始化为插件。
目前我在 XML 中定义的 <Socket>
appender 使用 org.apache.logging.log4j.core.appender.DefaultErrorHandler
但它打印了一些我不想出现在 catalina.out
中的信息。
log4j2.xml
这是我目前拥有的:
<Appenders>
<Socket name="socketLoggerAdapter" host="my-server" port="4006" protocol="UDP" reconnectionDelayMillis="60000">
<JsonLayout properties="true"/>
</Socket>
...
<Appenders>
我希望我可以添加一个子元素 <handler class="com.mycompany.MyErrorHandler">
但我在 Log4j 2 文档中找不到任何解决这个用例的内容。
有人可以指点我一些文档或提供有关如何实现它的基本步骤吗?
我要压抑的东西
我的 catalina.out
在 tomcat 启动期间偶尔会记录这个,我不希望它被报告:
2020-06-30 18:12:38,238 AsyncAppender-myAppenderRef-socketAppender ERROR An exception occurred processing Appender socketAppender org.apache.logging.log4j.core.appender.AppenderLoggingException: Error flushing stream UDP:myserver:9006
at org.apache.logging.log4j.core.appender.OutputStreamManager.flush(OutputStreamManager.java:176)
at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:107)
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:165)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:138)
at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:129)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:97)
at com.mycompany.MyCustomAppender$AsyncThread.callAppender(MyCustomAppender.java:455)
at com.mycompany.MyCustomAppender$AsyncThread.run(MyCustomAppender.java:398)
Caused by: java.io.IOException: Message too long
... 8 more
其他信息
错误处理程序的初始化方式如下:
org.apache.logging.log4j.core.appender.DefaultErrorHandler.<init>(org.apache.logging.log4j.core.Appender) line: 47
org.apache.logging.log4j.core.appender.ConsoleAppender(org.apache.logging.log4j.core.appender.AbstractAppender).<init>(java.lang.String, org.apache.logging.log4j.core.Filter, org.apache.logging.log4j.core.Layout<? extends java.io.Serializable>, boolean) line: 40
org.apache.logging.log4j.core.appender.ConsoleAppender(org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender<M>).<init>(java.lang.String, org.apache.logging.log4j.core.Layout<? extends java.io.Serializable>, org.apache.logging.log4j.core.Filter, boolean, boolean, M) line: 60
org.apache.logging.log4j.core.appender.ConsoleAppender.<init>(java.lang.String, org.apache.logging.log4j.core.Layout<? extends java.io.Serializable>, org.apache.logging.log4j.core.Filter, org.apache.logging.log4j.core.appender.OutputStreamManager, boolean) line: 69
org.apache.logging.log4j.core.appender.ConsoleAppender.createDefaultAppenderForLayout(org.apache.logging.log4j.core.Layout<? extends java.io.Serializable>) line: 106
org.apache.logging.log4j.core.config.DefaultConfiguration.<init>() line: 62
org.apache.logging.log4j.core.LoggerContext.<init>(java.lang.String, java.lang.Object, java.net.URI) line: 75
org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.locateContext(java.lang.ClassLoader, java.net.URI) line: 145
org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(java.lang.String, java.lang.ClassLoader, boolean, java.net.URI) line: 70
org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(java.lang.String, java.lang.ClassLoader, boolean) line: 57
org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(java.lang.String, java.lang.ClassLoader, java.lang.Object, boolean) line: 141
org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(java.lang.String, java.lang.ClassLoader, java.lang.Object, boolean) line: 41
org.apache.logging.log4j.LogManager.getContext(java.lang.ClassLoader, boolean) line: 185
org.apache.logging.slf4j.Log4jLoggerFactory(org.apache.logging.log4j.spi.AbstractLoggerAdapter<L>).getContext(java.lang.Class<?>) line: 103
org.apache.logging.slf4j.Log4jLoggerFactory.getContext() line: 43
org.apache.logging.slf4j.Log4jLoggerFactory(org.apache.logging.log4j.spi.AbstractLoggerAdapter<L>).getLogger(java.lang.String) line: 42
org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(java.lang.String) line: 29
org.slf4j.LoggerFactory.getLogger(java.lang.String) line: 358
org.apache.log4j.Logger(org.apache.log4j.Category).<init>(java.lang.String) line: 57
org.apache.log4j.Logger.<init>(java.lang.String) line: 37
org.apache.log4j.Log4jLoggerFactory.getLogger(java.lang.String) line: 43
org.apache.log4j.Logger.getLogger(java.lang.String) line: 41
org.apache.log4j.Logger.getLogger(java.lang.Class) line: 49
com.mycompany.core.security.SessionUtil.<clinit>() line: 24
com.mycompany.MyCompanyContextLoaderListener.contextInitialized(javax.servlet.ServletContextEvent) line: 14
org.apache.catalina.core.StandardContext.listenerStart() line: 4812
org.apache.catalina.core.StandardContext.startInternal() line: 5255
org.apache.catalina.core.StandardContext(org.apache.catalina.util.LifecycleBase).start() line: 150
org.apache.catalina.core.StandardHost(org.apache.catalina.core.ContainerBase).addChildInternal(org.apache.catalina.Container) line: 725
org.apache.catalina.core.StandardHost(org.apache.catalina.core.ContainerBase).addChild(org.apache.catalina.Container) line: 701
org.apache.catalina.core.StandardHost.addChild(org.apache.catalina.Container) line: 717
org.apache.catalina.startup.HostConfig.deployWAR(org.apache.catalina.util.ContextName, java.io.File) line: 945
org.apache.catalina.startup.HostConfig$DeployWar.run() line: 1795
java.util.concurrent.Executors$RunnableAdapter<T>.call() line: 471
java.util.concurrent.FutureTask<V>.run() line: 262
java.util.concurrent.ThreadPoolExecutor.runWorker(java.util.concurrent.ThreadPoolExecutor$Worker) line: 1145
java.util.concurrent.ThreadPoolExecutor$Worker.run() line: 615
java.lang.Thread.run() line: 745
我相信最近还有另一个关于此的 Whosebug 问题。这是我的疏忽,您应该为此创建一个 Jira 问题。虽然您可以调用 setErrorHandler 方法来更改 ErrorHandler,但无法配置一个,因为它未初始化为插件。