You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Gernot Hueller <g....@gmx.at> on 2022/03/24 06:34:22 UTC

manual log4j2 initialization inside webapp ends up in extra LoggerContext

Dear all,

I am at a dead end, I hope someone on this mailing list can help me...



I have a web application deployed as an EAR in a JBoss application server, containing of a .war and many utility jars (managed by gradle)

For various complex reasons, I cannot use "automagic" configuration. Mostly, I set a few System properties in the code before initialization.

Initialization is done in a servlet which is guaranteed to start first by WEB-INF/web.xml

Initialization sets system properties then runs

            Configurator.initialize(classLoader, new ConfigurationSource(new FileInputStream(filename)));

or alternatively REconfigure following the FAQ at https://logging.apache.org/log4j/log4j-2.4/faq.html



However, no matter what I do, I end up with TWO LoggerContext instances

(I see that during initialization trace and later in log4jContextFactory.getSelector().getLoggerContexts())

and the "wrong" LoggerContext has a RootLogger with level ERROR and NO appenders and all of the loggers that are actually used and touched in the application are under the "wrong" LoggerContext and have log level ERROR and do not log. Only some loggers which are directly used in the web application .war are configured correctly.



for all the "wrong" loggers I get a message like

loggerContext name=24421690 - logger name=org.apache.velocity.loader.webapp level=ERROR appenders=

WARN StatusLogger The Logger  was created with the message factory org.apache.logging.log4j.message.ParameterizedMessageFactory@63e37b6c and is now requested with the message factory org.apache.logging.log4j.message.ParameterizedMessageFactory@19334714, which may create log events with unexpected formatting.



I did not ask for a second LoggerContext and I do not understand why it is created.

In fact is is created inside the loggerContext.setConfigLocation(file.toURI()) of the "only" LoggerContext I know





Please help me initialize my log4j properly from a ConfigurationSource







Here is some trace log, where the "wrong" LoggerContext is created without a configuration - and all subsequent loggers are put under THAT loggerContext



DEBUG StatusLogger Registering MBean org.apache.logging.log4j2:type=56df3650,component=Appenders,name=msgInFile

DEBUG StatusLogger Registering MBean org.apache.logging.log4j2:type=56df3650,component=Appenders,name=msgOutFile

TRACE StatusLogger Using DummyNanoClock for nanosecond timestamps.

DEBUG StatusLogger Reconfiguration complete for context[name=56df3650] at URI C:\Java\myprj\conf\gernot_log4j2\log4j2.xml (org.apache.logging.log4j.core.LoggerContext@f034b2) with optional ClassLoader: null

DEBUG StatusLogger PluginManager 'Lookup' found 16 plugins

DEBUG StatusLogger PluginManager 'Converter' found 47 plugins

DEBUG StatusLogger Starting OutputStreamManager SYSTEM_OUT.false.false-3

DEBUG StatusLogger Starting LoggerContext[name=7ff11457, org.apache.logging.log4j.core.LoggerContext@1c50492]...

DEBUG StatusLogger Reconfiguration started for context[name=7ff11457] at URI null (org.apache.logging.log4j.core.LoggerContext@1c50492) with optional ClassLoader: null

DEBUG StatusLogger Using configurationFactory org.apache.logging.log4j.core.config.ConfigurationFactory$Factory@2c01ff02

TRACE StatusLogger Trying to find [log4j2-test7ff11457.properties] using context class loader ModuleClassLoader for Module "deployment.aceadmin.ear.CampaignManagement-WebApp.war:main" from Service Module Loader.

...

TRACE StatusLogger Trying to find [log4j2.xml] using ModuleClassLoader for Module "deployment.aceadmin.ear.CampaignManagement-WebApp.war:main" from Service Module Loader class loader.

TRACE StatusLogger Trying to find [log4j2.xml] using ClassLoader.getSystemResource().

WARN StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2





---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: manual log4j2 initialization inside webapp ends up in extra LoggerContext

Posted by Ralph Goers <ra...@dslextreme.com>.
By default, Log4j 2 uses ClassLoaderContextSelector. The way this works is that whenever LogManager.getLogger() is called LogManager will call ClassLoaderContextSelector which will locate the LoggerContext associated with the ClassLoader of the class that called LogManager.getLogger(). In web container such as Tomcat this allows the Log4j jars to be placed in Tomcat’s classloader and a separate LoggerContext will be created for Tomcat as well as each web application. Each would have its own logging configuration.

So when log4j is initialized by your servlet the LoggerContext created will only apply to the web app that contains that servlet. If you also have an EAR it would also have its own ClassLoader and so could have its own logging configuration.

If you only want a single LoggerContext for the whole JBoss then you need to switch to a different ContextSelector. https://logging.apache.org/log4j/2.x/manual/extending.html#ContextSelector lists the various ContextSelectors provided by Log4j but you can also write your own if you want something custom.

If you want you can add the property that specifies the ContextSelector class name to a file named log4j2.component.properties that you then place on the class path. Otherwise you would specify it as a system property passed to your application.

Ralph

> On Mar 23, 2022, at 11:34 PM, Gernot Hueller <g....@gmx.at> wrote:
> 
> Dear all,
> 
> I am at a dead end, I hope someone on this mailing list can help me...
> 
> 
> 
> I have a web application deployed as an EAR in a JBoss application server, containing of a .war and many utility jars (managed by gradle)
> 
> For various complex reasons, I cannot use "automagic" configuration. Mostly, I set a few System properties in the code before initialization.
> 
> Initialization is done in a servlet which is guaranteed to start first by WEB-INF/web.xml
> 
> Initialization sets system properties then runs
> 
>            Configurator.initialize(classLoader, new ConfigurationSource(new FileInputStream(filename)));
> 
> or alternatively REconfigure following the FAQ at https://logging.apache.org/log4j/log4j-2.4/faq.html
> 
> 
> 
> However, no matter what I do, I end up with TWO LoggerContext instances
> 
> (I see that during initialization trace and later in log4jContextFactory.getSelector().getLoggerContexts())
> 
> and the "wrong" LoggerContext has a RootLogger with level ERROR and NO appenders and all of the loggers that are actually used and touched in the application are under the "wrong" LoggerContext and have log level ERROR and do not log. Only some loggers which are directly used in the web application .war are configured correctly.
> 
> 
> 
> for all the "wrong" loggers I get a message like
> 
> loggerContext name=24421690 - logger name=org.apache.velocity.loader.webapp level=ERROR appenders=
> 
> WARN StatusLogger The Logger  was created with the message factory org.apache.logging.log4j.message.ParameterizedMessageFactory@63e37b6c and is now requested with the message factory org.apache.logging.log4j.message.ParameterizedMessageFactory@19334714, which may create log events with unexpected formatting.
> 
> 
> 
> I did not ask for a second LoggerContext and I do not understand why it is created.
> 
> In fact is is created inside the loggerContext.setConfigLocation(file.toURI()) of the "only" LoggerContext I know
> 
> 
> 
> 
> 
> Please help me initialize my log4j properly from a ConfigurationSource
> 
> 
> 
> 
> 
> 
> 
> Here is some trace log, where the "wrong" LoggerContext is created without a configuration - and all subsequent loggers are put under THAT loggerContext
> 
> 
> 
> DEBUG StatusLogger Registering MBean org.apache.logging.log4j2:type=56df3650,component=Appenders,name=msgInFile
> 
> DEBUG StatusLogger Registering MBean org.apache.logging.log4j2:type=56df3650,component=Appenders,name=msgOutFile
> 
> TRACE StatusLogger Using DummyNanoClock for nanosecond timestamps.
> 
> DEBUG StatusLogger Reconfiguration complete for context[name=56df3650] at URI C:\Java\myprj\conf\gernot_log4j2\log4j2.xml (org.apache.logging.log4j.core.LoggerContext@f034b2) with optional ClassLoader: null
> 
> DEBUG StatusLogger PluginManager 'Lookup' found 16 plugins
> 
> DEBUG StatusLogger PluginManager 'Converter' found 47 plugins
> 
> DEBUG StatusLogger Starting OutputStreamManager SYSTEM_OUT.false.false-3
> 
> DEBUG StatusLogger Starting LoggerContext[name=7ff11457, org.apache.logging.log4j.core.LoggerContext@1c50492]...
> 
> DEBUG StatusLogger Reconfiguration started for context[name=7ff11457] at URI null (org.apache.logging.log4j.core.LoggerContext@1c50492) with optional ClassLoader: null
> 
> DEBUG StatusLogger Using configurationFactory org.apache.logging.log4j.core.config.ConfigurationFactory$Factory@2c01ff02
> 
> TRACE StatusLogger Trying to find [log4j2-test7ff11457.properties] using context class loader ModuleClassLoader for Module "deployment.aceadmin.ear.CampaignManagement-WebApp.war:main" from Service Module Loader.
> 
> ...
> 
> TRACE StatusLogger Trying to find [log4j2.xml] using ModuleClassLoader for Module "deployment.aceadmin.ear.CampaignManagement-WebApp.war:main" from Service Module Loader class loader.
> 
> TRACE StatusLogger Trying to find [log4j2.xml] using ClassLoader.getSystemResource().
> 
> WARN StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2
> 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org