log4j2:为异步日志记录设置Log4jContextSelector系统属性的位置

我试图在当前在自由配置文件服务器中运行的REST Web方法中设置异步日志记录(出于性能原因)。

为此,我设置了以下属性:

System.setProperty("Log4jContextSelector", "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector"); 

我的问题是,无论我在哪里这样做,有时它的工作和记录非常快,有时它不会。

我已经尝试了(a)包含所有REST Web方法(b)的类的构造函数,该方法在REST方法之前在REST方法(d)中在REST方法本身(d)中调用

这些位置都不一致。

任何人都可以提供此行为的解释,如果可能的话,建议的方法来解决问题。

编辑:似乎在调用setProperty之前初始化了log4j。 所以我需要做的是通过自由配置文件来设置属性。

有一种未记录的方法为您的项目设置此值,而无需在启动期间手动传入系统属性值。

将名为log4j2.component.properties的文件添加到类路径中。 这可以在大多数maven或gradle项目中通过将其保存在src/main/resources

这个文件只是java.util.Properties文件。 通过将以下行添加到文件来设置上下文选择器的值。

 Log4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector 

Log4j将首先尝试读取系统属性。 如果系统属性为null,则默认情况下它将回退到存储在此文件中的值。

执行此设置的代码位于Log4jContextFactory.java:91 。

文件位置

我的问题是,无论我在哪里这样做,有时它的工作和记录非常快,有时它不会。

将该代码添加到定义主入口点的类中的静态初始化程序块中。

 public class MainClass { // NOTE: Nothing can appear before this initializer // NOTE: This initializer must be in the class that contains your entry point static { System.setProperty("Log4jContextSelector", "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector"); } public static void main(final String[] args) { // Do anything you want to here } } 

根据Java规范,静态初始化按其声明的顺序发生。 因此, System.setProperty调用保证在Log4j初始化之前发生。

显然我需要在jvm.options文件中添加一行

 -DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector 

jvm.options文件位于:

 ${server.config.dir}/jvm.options 

可以使用以下链接找到该目录:

http://www-01.ibm.com/support/knowledgecenter/SSEQTP_8.5.5/com.ibm.websphere.wlp.doc/ae/rwlp_dirs.html?cp=SSEQTP_8.5.5%2F1-3-11-0- 2-0

在我的例子中,它位于:C:\ eclipse \ runtime \ usr \ servers \ serverName

在调用main方法之前初始化了Log4j。 所以它无法从系统中选择你的属性Log4jContextSelector,默认情况下,它同步工作。

要validation相同:删除disruptor依赖项,如果你的项目仍在上升,那么它不会异步。

如果你通过-DLog4jContextSelector = org.apache.logging.log4j.core.async.AsyncLoggerContextSelector添加属性,那么删除disruptor项目后就不会上去了。

如果您使用的是tomcat,请在catalina.properties中添加系统属性。 并且不要忘记使用immediateFlush =“false”。