log4j2: Location for setting Log4jContextSelector system property for asynchronous logging

前端 未结 4 1911
被撕碎了的回忆
被撕碎了的回忆 2021-02-05 17:45

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 h

4条回答
  •  野的像风
    2021-02-05 18:33

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

    Add that code in a static initializer block in the class that defines your main 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 it is declared. Therefore, the System.setProperty call is guaranteed to happen before the Log4j initialization.

提交回复
热议问题