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 CARTEREAU Benjamin <be...@cnav.fr> on 2020/05/27 12:59:35 UTC

Different configuration files for the same ClassLoader

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.

RE: Different configuration files for the same ClassLoader

Posted by CARTEREAU Benjamin <be...@cnav.fr>.
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 <ra...@dslextreme.com>
Envoyé : mercredi 27 mai 2020 17:27
À : Log4J Users List <lo...@logging.apache.org>
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 <be...@cnav.fr> 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: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org

***************************************************************************************** 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: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Different configuration files for the same ClassLoader

Posted by Ralph Goers <ra...@dslextreme.com>.
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 <be...@cnav.fr> 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: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org