Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-3468

can we set log4j property 'Log4jContextSelector' per webapps war deployment



    • Question
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.17.2
    • None
    • Log4j 1.2 bridge
    • None


      In my application where I am using custom logger and custom ContextSelector log4j2 2.17.2 version, I have 2 processes running and there are 3 webapps war files which get deployed in tomcat version 9.0.60. Out of these 3 war files, one war file is internal to my application while other 2 are coming as final artifacts from artifiactory.

      My war uses a custom logger and custom ContextSelector, however other 2 wars from artifactory use standard logger and standard ContextSelector.


      I have defined below system property as a static block in custom logger.

      static {

          System.setProperty("Log4jContextSelector", "logger.log4j2.CustomLog4j2ContextSelector");


      However, logger gets initialized before the execution of above static block in custom logger and before the deployment of wars. And due to this log4j jars which are kept outside of war, don't find this property and during the deployment of my internal war file leads to ClassCastException "java.lang.ClassCastException: org.apache.logging.log4j.core.selector.ContextSelector cannot be cast to logger.log4j2.CustomLog4j2ContextSelector". To fix this I have placed 'log4j2.component.properties' file on log4j jars class path.

      Content of file 'log4j2.component.properties' -> Log4jContextSelector=logger.log4j2.CustomLog4j2ContextSelector

      But with this fix, for other 2 wars I get following below harmless error(wars functionality works fine) when I keep 'log4j2.component.properties' file on log4j jars class path.

      ERROR StatusLogger Unable to create custom ContextSelector. Falling back to default.

      "java.lang.ClassCastException: Cannot cast logger.log4j2.CustomLog4j2ContextSelector to org.apache.logging.log4j.core.selector.ContextSelector".


      I am looking for help to come up with a solution where, when my war gets deployed then custom logger context "logger.log4j2.CustomLog4j2ContextSelector" should be used while when other 2 wars get deployed then  "org.apache.logging.log4j.core.selector.ContextSelector" should be used. Please let me know if you have any idea on this.




            Unassigned Unassigned
            PoojaTheCoder Pooja Pandey
            0 Vote for this issue
            2 Start watching this issue