manual log4j2 initialization inside webapp ends up in extra LoggerContext

Gernot Hueller <[email protected]>
Newsgroups gmane.comp.jakarta.log4j.user
Message-ID <trinity-37dd721a-77cd-4b85-a0fd-a3493114685d-1648103662490@3c-app-gmx-bap17>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.