Log4j2: location for setting the Log4jContextSelector system property for asynchronous logging

I am trying to configure asynchronous logging (for performance reasons) as part of the REST web methods that are currently running on the freedom profile server.

To do this, I created the following property:

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

My problem is that no matter where I do it, sometimes it works, and registration is very fast, and sometimes not.

I tried (a) in the constructor for a class containing all the web REST methods (b) in the filter doFilter method, which is called before the REST method (c) in the filter initialization method (d) in the REST method itself

None of these places work in sequence.

Can anyone suggest an explanation for this behavior and, if possible, a suggested way to fix the problem.

EDIT: It seems that log4j is initialized before calling setProperty. So what I need to do is configure the property through the freedom profile.

+6
source share
4 answers

Therefore, apparently, I needed to add a line to the jvm.options file

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

The jvm.options file is here:

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

And this directory can be found at the link:

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

In my case, this is: C: \ eclipse \ runtime \ usr \ servers \ serverName

0
source

There is an undocumented method for setting this value for your project without having to manually pass the value of the system property at startup.

Add a file called log4j2.component.properties to your classpath. This can be done in most maven or gradle projects by storing it in src/main/resources .

This file is just java.util.Properties . Set the value for the context selector by adding the following line to the file.

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

Log4j will try to read the system property first. If the system property is NULL, then it defaults to the values ​​stored in this file.

The code that performs this setup is in Log4jContextFactory.java:91 .

File location

+11
source

My problem is that no matter where I do it, sometimes it works, and registration is very fast, and sometimes not.

Add this code to the static initializer block in the class that defines your primary entry point.

 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 } } 

According to the Java specification, static initialization occurs in the order in which it is declared. Consequently, a call to System.setProperty guaranteed to be executed prior to initializing Log4j.

+1
source

Log4j was initialized before calling the main method. Therefore, it cannot select your Log4jContextSelector property from the system and, by default, works synchronously.

To verify this: remove the crash dependency; if your project is still ongoing, it does not receive an asynchronous call.

If you add the property through -DLog4jContextSelector = org.apache.logging.log4j.core.async.AsyncLoggerContextSelector, then after deleting the project, disruptor will not increase.

If you are using tomcat then add system properties to catalina.properties. And don't forget to use instantFlush = "false".

0
source

Source: https://habr.com/ru/post/957702/


All Articles