Different configuration files for the same ClassLoader

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Different configuration files for the same ClassLoader

CARTEREAU Benjamin
Hello,
I am using log4j2 in a less common environment (webMethods IS java server).

The thing is that there is a common service that do the logging (using log4j2) for different "applications" (called "package" in that environment) within the same server.

I would like to know if there is a way, within the same common service/java code to initialize a different LoggerContext for each application and using a different "log4j2.xml" configuration file (these configuration files could use a different name "log4j2-A. xml", "log4j2-B. xml" or be put in different directories).

I have been trying to use:
LoggerContext contextA = Configurator.initialize(pkg, ClassLoader.getSystemClassLoader(), "log4j2-A.xml");
But when trying then to create a new context :
LoggerContext contextB = Configurator.initialize(pkg, ClassLoader.getSystemClassLoader(), "log4j2-B.xml");
But without success. I get the message :
WARN locateContext called with URI log4j2-B.xml. Existing LoggerContext has URI log4j2-A.xml

I would guess it is the expected behavior since I am using the defaut ContextSelector (ClassLoaderContextSelector) and I can only have one context (and one configuration file eventually).
Am I right ?
Or is there a way to achieve what I am looking for ?

Best regards.

Ben
***************************************************************************************** Ce message ?lectronique, les ?ventuels fichiers joints et les informations qu'il contient, peuvent ?tre couverts par le secret professionnel et sont exclusivement adress?s ? son destinataire. Si vous le recevez par erreur, merci d'en informer l'exp?diteur et de le d?truire. A ce titre, toute diffusion non autoris?e de ce message, m?me partielle, sous quelque forme que ce soit, est interdite et punie par la loi. En aucun cas, le contenu de ce message ne peut valoir offre ou acceptation ou engagement de la part de la CNAV. Les opinions contenues dans ce message sont personnelles ? l'?metteur. Il est de la responsabilit? du destinataire de s'assurer que le message et ses ?ventuelles pi?ces jointes ne contiennent pas de virus informatique.
Reply | Threaded
Open this post in threaded view
|

Re: Different configuration files for the same ClassLoader

Ralph Goers
When an application calls LogManager.getLogger() Log4j uses a ContextSelector to locate the appropriate LoggerContext. The default is the ClassLoaderContextSelector, which will use the LoggerContext associated with the ClassLoader of the class obtaining the Logger.

To do what you want you would need to create a new ContextSelector that can differentiate when it should use one LoggerContext over another. You would also need. To be sure that when each loads its configuration that it somehow can determine which configuration it should use.

Ralph

> On May 27, 2020, at 5:59 AM, CARTEREAU Benjamin <[hidden email]> wrote:
>
> Hello,
> I am using log4j2 in a less common environment (webMethods IS java server).
>
> The thing is that there is a common service that do the logging (using log4j2) for different "applications" (called "package" in that environment) within the same server.
>
> I would like to know if there is a way, within the same common service/java code to initialize a different LoggerContext for each application and using a different "log4j2.xml" configuration file (these configuration files could use a different name "log4j2-A. xml", "log4j2-B. xml" or be put in different directories).
>
> I have been trying to use:
> LoggerContext contextA = Configurator.initialize(pkg, ClassLoader.getSystemClassLoader(), "log4j2-A.xml");
> But when trying then to create a new context :
> LoggerContext contextB = Configurator.initialize(pkg, ClassLoader.getSystemClassLoader(), "log4j2-B.xml");
> But without success. I get the message :
> WARN locateContext called with URI log4j2-B.xml. Existing LoggerContext has URI log4j2-A.xml
>
> I would guess it is the expected behavior since I am using the defaut ContextSelector (ClassLoaderContextSelector) and I can only have one context (and one configuration file eventually).
> Am I right ?
> Or is there a way to achieve what I am looking for ?
>
> Best regards.
>
> Ben
> ***************************************************************************************** Ce message ?lectronique, les ?ventuels fichiers joints et les informations qu'il contient, peuvent ?tre couverts par le secret professionnel et sont exclusivement adress?s ? son destinataire. Si vous le recevez par erreur, merci d'en informer l'exp?diteur et de le d?truire. A ce titre, toute diffusion non autoris?e de ce message, m?me partielle, sous quelque forme que ce soit, est interdite et punie par la loi. En aucun cas, le contenu de ce message ne peut valoir offre ou acceptation ou engagement de la part de la CNAV. Les opinions contenues dans ce message sont personnelles ? l'?metteur. Il est de la responsabilit? du destinataire de s'assurer que le message et ses ?ventuelles pi?ces jointes ne contiennent pas de virus informatique.



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: Different configuration files for the same ClassLoader

CARTEREAU Benjamin
Thank you very much Ralph.
That is what I have come up with as a conclusion too.
Do you know if there are any examples (on github maybe) of this kind of custom ContextSelector?

Thanks again

Ben

-----Message d'origine-----
De : Ralph Goers <[hidden email]>
Envoyé : mercredi 27 mai 2020 17:27
À : Log4J Users List <[hidden email]>
Objet : Re: Different configuration files for the same ClassLoader

When an application calls LogManager.getLogger() Log4j uses a ContextSelector to locate the appropriate LoggerContext. The default is the ClassLoaderContextSelector, which will use the LoggerContext associated with the ClassLoader of the class obtaining the Logger.

To do what you want you would need to create a new ContextSelector that can differentiate when it should use one LoggerContext over another. You would also need. To be sure that when each loads its configuration that it somehow can determine which configuration it should use.

Ralph

> On May 27, 2020, at 5:59 AM, CARTEREAU Benjamin <[hidden email]> wrote:
>
> Hello,
> I am using log4j2 in a less common environment (webMethods IS java server).
>
> The thing is that there is a common service that do the logging (using log4j2) for different "applications" (called "package" in that environment) within the same server.
>
> I would like to know if there is a way, within the same common service/java code to initialize a different LoggerContext for each application and using a different "log4j2.xml" configuration file (these configuration files could use a different name "log4j2-A. xml", "log4j2-B. xml" or be put in different directories).
>
> I have been trying to use:
> LoggerContext contextA = Configurator.initialize(pkg,
> ClassLoader.getSystemClassLoader(), "log4j2-A.xml"); But when trying then to create a new context :
> LoggerContext contextB = Configurator.initialize(pkg,
> ClassLoader.getSystemClassLoader(), "log4j2-B.xml"); But without success. I get the message :
> WARN locateContext called with URI log4j2-B.xml. Existing
> LoggerContext has URI log4j2-A.xml
>
> I would guess it is the expected behavior since I am using the defaut ContextSelector (ClassLoaderContextSelector) and I can only have one context (and one configuration file eventually).
> Am I right ?
> Or is there a way to achieve what I am looking for ?
>
> Best regards.
>
> Ben
> ***************************************************************************************** Ce message ?lectronique, les ?ventuels fichiers joints et les informations qu'il contient, peuvent ?tre couverts par le secret professionnel et sont exclusivement adress?s ? son destinataire. Si vous le recevez par erreur, merci d'en informer l'exp?diteur et de le d?truire. A ce titre, toute diffusion non autoris?e de ce message, m?me partielle, sous quelque forme que ce soit, est interdite et punie par la loi. En aucun cas, le contenu de ce message ne peut valoir offre ou acceptation ou engagement de la part de la CNAV. Les opinions contenues dans ce message sont personnelles ? l'?metteur. Il est de la responsabilit? du destinataire de s'assurer que le message et ses ?ventuelles pi?ces jointes ne contiennent pas de virus informatique.



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

***************************************************************************************** Ce message électronique, les éventuels fichiers joints et les informations qu'il contient, peuvent être couverts par le secret professionnel et sont exclusivement adressés à son destinataire. Si vous le recevez par erreur, merci d'en informer l'expéditeur et de le détruire. A ce titre, toute diffusion non autorisée de ce message, même partielle, sous quelque forme que ce soit, est interdite et punie par la loi. En aucun cas, le contenu de ce message ne peut valoir offre ou acceptation ou engagement de la part de la CNAV. Les opinions contenues dans ce message sont personnelles à l'émetteur. Il est de la responsabilité du destinataire de s'assurer que le message et ses éventuelles pièces jointes ne contiennent pas de virus informatique.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]