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 Viral Shah <vs...@gmail.com> on 2012/08/29 21:18:20 UTC

Issue with setting the XMLConfiguration after the LoggerContext is initialized

Hello there,

 

I have started the migration process for a few of our applications running
on Weblogic 10.3.3 to Log4j 2.0. I am running into a few issues that I hope
someone can help me with.

 

Since all the apps are running within 1 JVM, each app is to read its own
log4j configuration and write to its own log file. To achieve this, I am
doing the following:

 

    private static void configure()

    {

        try

        {

            LoggerContext ctx = (LoggerContext)
LogManager.getContext(false);

            System.out.println("Original Config Name: " +
ctx.getConfiguration().getName());

 

            java.io.InputStream is =
Thread.currentThread().getContextClassLoader().getResourceAsStream(Constants
.PROP_LOG4J_FILENAME);

            InputSource isource = new InputSource(is);

            

            File file;

            URL url =
Thread.currentThread().getContextClassLoader().getResource(Constants.PROP_LO
G4J_FILENAME);

            try {

                file = new File(url.toURI());

            } catch(URISyntaxException e) {

                file = new File(url.getPath());

            }

            

            XMLConfiguration newConfig = new XMLConfiguration(isource,
file);

            System.out.println("New Config Name: " + newConfig.getName());

 

            ctx.setConfiguration(newConfig);

            ctx.reconfigure();

            System.out.println("CTX isReady: " + ctx.isStarted());

            

            Map<String, Appender> map =
ctx.getConfiguration().getAppenders();

            System.out.println("Current Config Map Size: " + map.size());

            System.out.println("Current Config Name: " +
ctx.getConfiguration().getName());

        }

        catch(Exception ex)

        {

            ex.printStackTrace();

        }

    }

 

The System output I get is as follows:

 

Original Config Name: Default

New Config Name: AccessService

CTX isReady: true

Current Config Map Size: 1

Current Config Name: Default

 

This tells me that the ctx.setConfiguration(newConfig) and the
ctx.reconfigure() did not actually take in the new configuration as
presented by the XMLConfiguration. How do I go about resolving this? 

 

I don't want to set a system level property since that will be applicable to
the entire VM and my loggers append to different log files rather than the
intended one. Please help me as I have been stuck on this for the past 4
days. I appreciate any pointers you can offer.

 

Thanks,

Viral Shah

AIM: budmiller75

Yahoo/Skype: virals3

 

 


Re: Issue with setting the XMLConfiguration after the LoggerContext is initialized

Posted by Ralph Goers <ra...@dslextreme.com>.
Looking at what you are doing I am thinking that when you update the configuration file automatic reconfiguration is probably not going to work properly as it calls the reconfigure method.  Since you provided the file it should be using that instead of the standard search sequence.

Ralph

On Aug 29, 2012, at 12:58 PM, Ralph Goers wrote:

> Since you are manually setting up the configuration and then calling setConfiguration() you don't want to call reconfigure(). setConfiguration() will perform the reconfiguration with the config you provided. reconfigure() will try to locate the configuration file using the normal mechanism and load it. Since it didn't find it it reset it back to the default.
> 
> FWIW - I had a hard time trying to figure out how to automate this use case when using the ClassLoaderContextSelector.  If you have any suggestions on how to make this work better they would be much appreciated.   
> 
> There is a JNDIContextFilter that will set up a LoggerContext per web app but you would need to use the JNDIContextSelector instead of the ClassLoaderContextSelector.
> 
> Ralph
> 
> On Aug 29, 2012, at 12:18 PM, Viral Shah wrote:
> 
>> Hello there,
>> 
>> 
>> 
>> I have started the migration process for a few of our applications running
>> on Weblogic 10.3.3 to Log4j 2.0. I am running into a few issues that I hope
>> someone can help me with.
>> 
>> 
>> 
>> Since all the apps are running within 1 JVM, each app is to read its own
>> log4j configuration and write to its own log file. To achieve this, I am
>> doing the following:
>> 
>> 
>> 
>>   private static void configure()
>> 
>>   {
>> 
>>       try
>> 
>>       {
>> 
>>           LoggerContext ctx = (LoggerContext)
>> LogManager.getContext(false);
>> 
>>           System.out.println("Original Config Name: " +
>> ctx.getConfiguration().getName());
>> 
>> 
>> 
>>           java.io.InputStream is =
>> Thread.currentThread().getContextClassLoader().getResourceAsStream(Constants
>> .PROP_LOG4J_FILENAME);
>> 
>>           InputSource isource = new InputSource(is);
>> 
>> 
>> 
>>           File file;
>> 
>>           URL url =
>> Thread.currentThread().getContextClassLoader().getResource(Constants.PROP_LO
>> G4J_FILENAME);
>> 
>>           try {
>> 
>>               file = new File(url.toURI());
>> 
>>           } catch(URISyntaxException e) {
>> 
>>               file = new File(url.getPath());
>> 
>>           }
>> 
>> 
>> 
>>           XMLConfiguration newConfig = new XMLConfiguration(isource,
>> file);
>> 
>>           System.out.println("New Config Name: " + newConfig.getName());
>> 
>> 
>> 
>>           ctx.setConfiguration(newConfig);
>> 
>>           ctx.reconfigure();
>> 
>>           System.out.println("CTX isReady: " + ctx.isStarted());
>> 
>> 
>> 
>>           Map<String, Appender> map =
>> ctx.getConfiguration().getAppenders();
>> 
>>           System.out.println("Current Config Map Size: " + map.size());
>> 
>>           System.out.println("Current Config Name: " +
>> ctx.getConfiguration().getName());
>> 
>>       }
>> 
>>       catch(Exception ex)
>> 
>>       {
>> 
>>           ex.printStackTrace();
>> 
>>       }
>> 
>>   }
>> 
>> 
>> 
>> The System output I get is as follows:
>> 
>> 
>> 
>> Original Config Name: Default
>> 
>> New Config Name: AccessService
>> 
>> CTX isReady: true
>> 
>> Current Config Map Size: 1
>> 
>> Current Config Name: Default
>> 
>> 
>> 
>> This tells me that the ctx.setConfiguration(newConfig) and the
>> ctx.reconfigure() did not actually take in the new configuration as
>> presented by the XMLConfiguration. How do I go about resolving this? 
>> 
>> 
>> 
>> I don't want to set a system level property since that will be applicable to
>> the entire VM and my loggers append to different log files rather than the
>> intended one. Please help me as I have been stuck on this for the past 4
>> days. I appreciate any pointers you can offer.
>> 
>> 
>> 
>> Thanks,
>> 
>> Viral Shah
>> 
>> AIM: budmiller75
>> 
>> Yahoo/Skype: virals3
>> 
>> 
>> 
>> 
>> 
> 


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


Re: Issue with setting the XMLConfiguration after the LoggerContext is initialized

Posted by Ralph Goers <ra...@dslextreme.com>.
Since you are manually setting up the configuration and then calling setConfiguration() you don't want to call reconfigure(). setConfiguration() will perform the reconfiguration with the config you provided. reconfigure() will try to locate the configuration file using the normal mechanism and load it. Since it didn't find it it reset it back to the default.

FWIW - I had a hard time trying to figure out how to automate this use case when using the ClassLoaderContextSelector.  If you have any suggestions on how to make this work better they would be much appreciated.   

There is a JNDIContextFilter that will set up a LoggerContext per web app but you would need to use the JNDIContextSelector instead of the ClassLoaderContextSelector.

Ralph

On Aug 29, 2012, at 12:18 PM, Viral Shah wrote:

> Hello there,
> 
> 
> 
> I have started the migration process for a few of our applications running
> on Weblogic 10.3.3 to Log4j 2.0. I am running into a few issues that I hope
> someone can help me with.
> 
> 
> 
> Since all the apps are running within 1 JVM, each app is to read its own
> log4j configuration and write to its own log file. To achieve this, I am
> doing the following:
> 
> 
> 
>    private static void configure()
> 
>    {
> 
>        try
> 
>        {
> 
>            LoggerContext ctx = (LoggerContext)
> LogManager.getContext(false);
> 
>            System.out.println("Original Config Name: " +
> ctx.getConfiguration().getName());
> 
> 
> 
>            java.io.InputStream is =
> Thread.currentThread().getContextClassLoader().getResourceAsStream(Constants
> .PROP_LOG4J_FILENAME);
> 
>            InputSource isource = new InputSource(is);
> 
> 
> 
>            File file;
> 
>            URL url =
> Thread.currentThread().getContextClassLoader().getResource(Constants.PROP_LO
> G4J_FILENAME);
> 
>            try {
> 
>                file = new File(url.toURI());
> 
>            } catch(URISyntaxException e) {
> 
>                file = new File(url.getPath());
> 
>            }
> 
> 
> 
>            XMLConfiguration newConfig = new XMLConfiguration(isource,
> file);
> 
>            System.out.println("New Config Name: " + newConfig.getName());
> 
> 
> 
>            ctx.setConfiguration(newConfig);
> 
>            ctx.reconfigure();
> 
>            System.out.println("CTX isReady: " + ctx.isStarted());
> 
> 
> 
>            Map<String, Appender> map =
> ctx.getConfiguration().getAppenders();
> 
>            System.out.println("Current Config Map Size: " + map.size());
> 
>            System.out.println("Current Config Name: " +
> ctx.getConfiguration().getName());
> 
>        }
> 
>        catch(Exception ex)
> 
>        {
> 
>            ex.printStackTrace();
> 
>        }
> 
>    }
> 
> 
> 
> The System output I get is as follows:
> 
> 
> 
> Original Config Name: Default
> 
> New Config Name: AccessService
> 
> CTX isReady: true
> 
> Current Config Map Size: 1
> 
> Current Config Name: Default
> 
> 
> 
> This tells me that the ctx.setConfiguration(newConfig) and the
> ctx.reconfigure() did not actually take in the new configuration as
> presented by the XMLConfiguration. How do I go about resolving this? 
> 
> 
> 
> I don't want to set a system level property since that will be applicable to
> the entire VM and my loggers append to different log files rather than the
> intended one. Please help me as I have been stuck on this for the past 4
> days. I appreciate any pointers you can offer.
> 
> 
> 
> Thanks,
> 
> Viral Shah
> 
> AIM: budmiller75
> 
> Yahoo/Skype: virals3
> 
> 
> 
> 
> 


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