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

Log4j API should allow specifying a LoggerContextFactory at runtime.



    • Bug
    • Status: Closed
    • Blocker
    • Resolution: Implemented
    • 2.0-rc2
    • None
    • API, Core


      The Problem

      LogManager uses a private static final LoggerContextFactory which is determined in the static initialization block. If the provider isn't available yet (e.g., in an OSGi environment), it will fall back to SimpleContextFactory and won't allow a different LoggerContextFactory to take its place when available.

      Proposed Solution

      The initialization code in LogManager will need to be refactored. The presence of a LoggerContextFactory implementation may not be available at the same time log4j-api is started. Thus, LogManager can shortcut to using SimpleContextFactory by default and then allow that to be overridden. This is currently a private static final field; I believe that it would be a better idea to use a sort of atomic reference pattern here (or volatile?). I'm not sure which way would be the fastest, but it shouldn't be final (I don't want to use reflection to force a different LoggerContextFactory like I'm doing for the JUL bridge I'm working on).

      In this scenario, it might be useful for the Loggers and LoggerContexts returned by SimpleContextFactory to be proxy classes so that they use whatever the active logger provider is. This way, if a client gets a Logger before log4j-core is activated, it can still use the proper logging objects. I don't know how much this affects performance, but java.util.logging uses a similar strategy for its loggers before java.util.logging.LogManager is initialized.

      Anyway, the main thing to do this here for is to allow all the other log4j JARs to not have to be fragments. This will allow for more robust OSGi integration and support in log4j-core later on (e.g., registering plugins through the OSGi service registry). It might also aid in some neat Spring integration, too (i.e., the ability to configure your loggers and appenders and such through Spring), but that's a separate idea.


        Issue Links



              mattsicker Matt Sicker
              mattsicker Matt Sicker
              0 Vote for this issue
              1 Start watching this issue