James Hutchinson James Hutchinson - 3 months ago 116
Java Question

log4j2: Location for setting Log4jContextSelector system property for asynchronous logging

I am attempting to set up asynchronous logging (for performance reasons) within REST web methods that currently run in a liberty profile server.

In order to do this, I have set up the following property:

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


My problem is that no matter where I do this, sometimes it works and logging is very fast, and sometimes it doesn't.

I have tried (a) in the constructor for the class containing all the REST web methods
(b) in the filter doFilter method that gets called prior to the REST method
(c) in the filter init method
(d) in the REST method itself

None of these locations work consistently.

Can anyone offer an explanation for this behaviour, and if possible a suggested way of fixing the problem.

EDIT: It appears that log4j is being initialised prior to calling setProperty. So what I need to do is set up the property via liberty profile instead.

Answer

So 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 located here:

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

And that directory can be found by using 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 it is at: C:\eclipse\runtime\usr\servers\serverName