Java – log4j2: the location where the log4jcontextselector system property is set for asynchronous logging
I tried to set asynchronous logging in the rest web method currently running in the free profile server (for performance reasons)
To do this, I set the following properties:
System.setProperty("Log4jContextSelector","org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
My problem is that no matter where I do this, sometimes it works and records very fast, sometimes it won't
I have tried (a) (b) in the constructor of the class containing all REST Web methods (c) in the filter doFilter method before the REST method, in the filter initialization method (d) in the REST method itself.
These positions are inconsistent
Anyone can provide an explanation of this behavior and, if possible, suggest ways to solve the problem
Edit: it seems that log4j. Is initialized before calling setproperty So what I need to do is set the properties through the free configuration file
Solution
There is an unrecorded way to set this value for your project without manually passing in the system property value during startup
Will be named log4j2 component. Add the properties file to the classpath This can be done in most Maven or gradle projects by saving it in Src / main / resources
This file is just Java util. Properties file Set the value of the context selector by adding the following line to the file
Log4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
Log4j will first attempt to read system properties If the system property is null, it will fall back to the value stored in this file by default
The code that performs this setting is located in log4jcontextfactory java:91.