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 Preetam Palwe <pr...@aftek.com> on 2009/06/21 10:41:20 UTC

Changing log4j configuration runtime ...

Hello all,

 

I have successfully configured log4j in my standalone java application
using PropertyConfigurator.configure()

Here is the configuration file 

 

log4j.rootLogger=DBUG, LogFile

 

log4j.logger.com.server.core=INFO

log4j.logger. com.server.core.SecurityManager =DEBUG

log4j.logger. com.server.core.SecurityPatternsCreator =DEBUG

 

 
log4j.appender.LogFile=org.apache.log4j.RollingFileAppender

                log4j.appender.LogFile.File=c:/server.log

                log4j.appender.LogFile.MaxFileSize=5MB

                log4j.appender.LogFile.MaxBackupIndex=10

 
log4j.appender.LogFile.layout=org.apache.log4j.PatternLayout

                log4j.appender.LogFile.layout.ConversionPattern=%d{yyyy
MM dd HH:mm:ss:SSS}# [%-5p]# [%t]# %c# %C{1}.%M# %m%n

 

In case of my application I have a security related library
(securitylib.jar) in which there we do some is a critical security
related processing. And I want to set logging level of "INFO" to all the
classes/loggers from this jar.

The log4j configuration is done before loading securitylib.jar using
above configuration file.

When securitylib.jar is loaded I tried to re-configure log4j using
following code.

(what I have done here is removed all the explicit loggers set in
configuration file, updated the configuration properties and configured
logger again)

 

But when I test the system I am still able to see the DEBUG logs for
classes/loggers com.server.core.SecurityManager and
com.server.core.SecurityPatternsCreator (which I had marked as DEBUG in
config files)

 

My questions are ...

1.       Why after reconfiguring the loggers the new configuration is
not getting reflected ?

2.       Is there any way to get the current log4j configuration and
change it at runtime ?

 

Thanks in advance for your help!

~PP

 

private void reinitLog4j()

    {

                try

                {

                    String log4jFile = System.getProperty("log4j.file");

                    Properties properties = new Properties();

                    properties.load(new FileInputStream(log4jFile));

                    updateLoggingConfiguration(properties);

                    PropertyConfigurator.configure(properties);

                }

                catch (IOException ioEx)

                {

                    throw new RuntimeException("failed to reconfigure
logger "

                                    + ioEx.getMessage(), ioEx);

                }

                logger = Logger.getLogger(this.getClass());

                logger.info("Log4j RE initialised to use INFO logs
everywhere !");

                logger.debug("THIS SHOULD NEVER COME IN LOGS");

    }

 

    private void updateLoggingConfiguration(Properties properties)

    {

                for (Iterator iterator =
properties.entrySet().iterator(); iterator

                                .hasNext();)

                {

                    Entry entry = (Entry) iterator.next();

                    String key = (String) entry.getKey();

                    if (key.equalsIgnoreCase("log4j.rootLogger"))

                    {

 
entry.setValue(getRootLoggerConfiguration((String) entry

                                                .getValue()));

                    }

                    else if (key.startsWith("log4j.logger"))

                    {

                                iterator.remove();

                    }

                }

    }

 

    private String getRootLoggerConfiguration(String currentConfig)

    {

                String[] tokens = IOUtil.tokenizeString(currentConfig,
",");

                StringBuilder builder = new StringBuilder();

                for (int i = 0; i < tokens.length; i++)

                {

                    if (i == 0)

                    {

                                builder.append("INFO");

                                continue;

                    }

                    builder.append(", " + tokens[i]);

                }

                return builder.toString();

    }

 

 

 


RE: Changing log4j configuration runtime ...

Posted by Preetam Palwe <pr...@aftek.com>.
It worked!!!

Just added ... 
properties.put("log4j.reset", "true");


Thanks !!!


-----Original Message-----
From: Preetam Palwe [mailto:preetamp@aftek.com] 
Sent: Monday, June 22, 2009 1:23 PM
To: Log4J Users List
Subject: RE: Changing log4j configuration runtime ...

I tried using 
new PropertyConfigurator().doConfigure(properties, LogManager
		    .getLoggerRepository());

But still the configuration is not getting reflected !

Any idea?




-----Original Message-----
From: Preetam Palwe [mailto:preetamp@aftek.com] 
Sent: Monday, June 22, 2009 12:54 PM
To: 'Log4J Users List'
Subject: RE: Changing log4j configuration runtime ...

Or in other words 
I want to do 

PropertyConfigurator.reconfigure(Properties prop);

Is there any way to do this ?



-----Original Message-----
From: Preetam Palwe [mailto:preetamp@aftek.com] 
Sent: Monday, June 22, 2009 12:13 PM
To: 'Log4J Users List'
Subject: RE: Changing log4j configuration runtime ...

Thanks Yair for the reply!

I have used property file only. 
The thing is the property file is open and if someone edit it and restart the java process he would be able to see all the DEBUG logs (which contains some critical information)

I dont want this to happen so want to change at runtime (in code) the configuration!


-----Original Message-----
From: Yair Ogen [mailto:yairogen@gmail.com] 
Sent: Sunday, June 21, 2009 2:38 PM
To: Log4J Users List
Subject: Re: Changing log4j configuration runtime ...

Why not use a regular property file?

Then when you to update the file you can load the changes automatically
using the "PropertyConfigurator.configureAndWatch" mechanism.


On Sun, Jun 21, 2009 at 11:41 AM, Preetam Palwe <pr...@aftek.com> wrote:

> Hello all,
>
>
>
> I have successfully configured log4j in my standalone java application
> using PropertyConfigurator.configure()
>
> Here is the configuration file
>
>
>
> log4j.rootLogger=DBUG, LogFile
>
>
>
> log4j.logger.com.server.core=INFO
>
> log4j.logger. com.server.core.SecurityManager =DEBUG
>
> log4j.logger. com.server.core.SecurityPatternsCreator =DEBUG
>
>
>
>
> log4j.appender.LogFile=org.apache.log4j.RollingFileAppender
>
>                log4j.appender.LogFile.File=c:/server.log
>
>                log4j.appender.LogFile.MaxFileSize=5MB
>
>                log4j.appender.LogFile.MaxBackupIndex=10
>
>
> log4j.appender.LogFile.layout=org.apache.log4j.PatternLayout
>
>                log4j.appender.LogFile.layout.ConversionPattern=%d{yyyy
> MM dd HH:mm:ss:SSS}# [%-5p]# [%t]# %c# %C{1}.%M# %m%n
>
>
>
> In case of my application I have a security related library
> (securitylib.jar) in which there we do some is a critical security
> related processing. And I want to set logging level of "INFO" to all the
> classes/loggers from this jar.
>
> The log4j configuration is done before loading securitylib.jar using
> above configuration file.
>
> When securitylib.jar is loaded I tried to re-configure log4j using
> following code.
>
> (what I have done here is removed all the explicit loggers set in
> configuration file, updated the configuration properties and configured
> logger again)
>
>
>
> But when I test the system I am still able to see the DEBUG logs for
> classes/loggers com.server.core.SecurityManager and
> com.server.core.SecurityPatternsCreator (which I had marked as DEBUG in
> config files)
>
>
>
> My questions are ...
>
> 1.       Why after reconfiguring the loggers the new configuration is
> not getting reflected ?
>
> 2.       Is there any way to get the current log4j configuration and
> change it at runtime ?
>
>
>
> Thanks in advance for your help!
>
> ~PP
>
>
>
> private void reinitLog4j()
>
>    {
>
>                try
>
>                {
>
>                    String log4jFile = System.getProperty("log4j.file");
>
>                    Properties properties = new Properties();
>
>                    properties.load(new FileInputStream(log4jFile));
>
>                    updateLoggingConfiguration(properties);
>
>                    PropertyConfigurator.configure(properties);
>
>                }
>
>                catch (IOException ioEx)
>
>                {
>
>                    throw new RuntimeException("failed to reconfigure
> logger "
>
>                                    + ioEx.getMessage(), ioEx);
>
>                }
>
>                logger = Logger.getLogger(this.getClass());
>
>                logger.info("Log4j RE initialised to use INFO logs
> everywhere !");
>
>                logger.debug("THIS SHOULD NEVER COME IN LOGS");
>
>    }
>
>
>
>    private void updateLoggingConfiguration(Properties properties)
>
>    {
>
>                for (Iterator iterator =
> properties.entrySet().iterator(); iterator
>
>                                .hasNext();)
>
>                {
>
>                    Entry entry = (Entry) iterator.next();
>
>                    String key = (String) entry.getKey();
>
>                    if (key.equalsIgnoreCase("log4j.rootLogger"))
>
>                    {
>
>
> entry.setValue(getRootLoggerConfiguration((String) entry
>
>                                                .getValue()));
>
>                    }
>
>                    else if (key.startsWith("log4j.logger"))
>
>                    {
>
>                                iterator.remove();
>
>                    }
>
>                }
>
>    }
>
>
>
>    private String getRootLoggerConfiguration(String currentConfig)
>
>    {
>
>                String[] tokens = IOUtil.tokenizeString(currentConfig,
> ",");
>
>                StringBuilder builder = new StringBuilder();
>
>                for (int i = 0; i < tokens.length; i++)
>
>                {
>
>                    if (i == 0)
>
>                    {
>
>                                builder.append("INFO");
>
>                                continue;
>
>                    }
>
>                    builder.append(", " + tokens[i]);
>
>                }
>
>                return builder.toString();
>
>    }
>
>
>
>
>
>
>
>



---------------------------------------------------------------------
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





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


RE: Changing log4j configuration runtime ...

Posted by Preetam Palwe <pr...@aftek.com>.
I tried using 
new PropertyConfigurator().doConfigure(properties, LogManager
		    .getLoggerRepository());

But still the configuration is not getting reflected !

Any idea?




-----Original Message-----
From: Preetam Palwe [mailto:preetamp@aftek.com] 
Sent: Monday, June 22, 2009 12:54 PM
To: 'Log4J Users List'
Subject: RE: Changing log4j configuration runtime ...

Or in other words 
I want to do 

PropertyConfigurator.reconfigure(Properties prop);

Is there any way to do this ?



-----Original Message-----
From: Preetam Palwe [mailto:preetamp@aftek.com] 
Sent: Monday, June 22, 2009 12:13 PM
To: 'Log4J Users List'
Subject: RE: Changing log4j configuration runtime ...

Thanks Yair for the reply!

I have used property file only. 
The thing is the property file is open and if someone edit it and restart the java process he would be able to see all the DEBUG logs (which contains some critical information)

I dont want this to happen so want to change at runtime (in code) the configuration!


-----Original Message-----
From: Yair Ogen [mailto:yairogen@gmail.com] 
Sent: Sunday, June 21, 2009 2:38 PM
To: Log4J Users List
Subject: Re: Changing log4j configuration runtime ...

Why not use a regular property file?

Then when you to update the file you can load the changes automatically
using the "PropertyConfigurator.configureAndWatch" mechanism.


On Sun, Jun 21, 2009 at 11:41 AM, Preetam Palwe <pr...@aftek.com> wrote:

> Hello all,
>
>
>
> I have successfully configured log4j in my standalone java application
> using PropertyConfigurator.configure()
>
> Here is the configuration file
>
>
>
> log4j.rootLogger=DBUG, LogFile
>
>
>
> log4j.logger.com.server.core=INFO
>
> log4j.logger. com.server.core.SecurityManager =DEBUG
>
> log4j.logger. com.server.core.SecurityPatternsCreator =DEBUG
>
>
>
>
> log4j.appender.LogFile=org.apache.log4j.RollingFileAppender
>
>                log4j.appender.LogFile.File=c:/server.log
>
>                log4j.appender.LogFile.MaxFileSize=5MB
>
>                log4j.appender.LogFile.MaxBackupIndex=10
>
>
> log4j.appender.LogFile.layout=org.apache.log4j.PatternLayout
>
>                log4j.appender.LogFile.layout.ConversionPattern=%d{yyyy
> MM dd HH:mm:ss:SSS}# [%-5p]# [%t]# %c# %C{1}.%M# %m%n
>
>
>
> In case of my application I have a security related library
> (securitylib.jar) in which there we do some is a critical security
> related processing. And I want to set logging level of "INFO" to all the
> classes/loggers from this jar.
>
> The log4j configuration is done before loading securitylib.jar using
> above configuration file.
>
> When securitylib.jar is loaded I tried to re-configure log4j using
> following code.
>
> (what I have done here is removed all the explicit loggers set in
> configuration file, updated the configuration properties and configured
> logger again)
>
>
>
> But when I test the system I am still able to see the DEBUG logs for
> classes/loggers com.server.core.SecurityManager and
> com.server.core.SecurityPatternsCreator (which I had marked as DEBUG in
> config files)
>
>
>
> My questions are ...
>
> 1.       Why after reconfiguring the loggers the new configuration is
> not getting reflected ?
>
> 2.       Is there any way to get the current log4j configuration and
> change it at runtime ?
>
>
>
> Thanks in advance for your help!
>
> ~PP
>
>
>
> private void reinitLog4j()
>
>    {
>
>                try
>
>                {
>
>                    String log4jFile = System.getProperty("log4j.file");
>
>                    Properties properties = new Properties();
>
>                    properties.load(new FileInputStream(log4jFile));
>
>                    updateLoggingConfiguration(properties);
>
>                    PropertyConfigurator.configure(properties);
>
>                }
>
>                catch (IOException ioEx)
>
>                {
>
>                    throw new RuntimeException("failed to reconfigure
> logger "
>
>                                    + ioEx.getMessage(), ioEx);
>
>                }
>
>                logger = Logger.getLogger(this.getClass());
>
>                logger.info("Log4j RE initialised to use INFO logs
> everywhere !");
>
>                logger.debug("THIS SHOULD NEVER COME IN LOGS");
>
>    }
>
>
>
>    private void updateLoggingConfiguration(Properties properties)
>
>    {
>
>                for (Iterator iterator =
> properties.entrySet().iterator(); iterator
>
>                                .hasNext();)
>
>                {
>
>                    Entry entry = (Entry) iterator.next();
>
>                    String key = (String) entry.getKey();
>
>                    if (key.equalsIgnoreCase("log4j.rootLogger"))
>
>                    {
>
>
> entry.setValue(getRootLoggerConfiguration((String) entry
>
>                                                .getValue()));
>
>                    }
>
>                    else if (key.startsWith("log4j.logger"))
>
>                    {
>
>                                iterator.remove();
>
>                    }
>
>                }
>
>    }
>
>
>
>    private String getRootLoggerConfiguration(String currentConfig)
>
>    {
>
>                String[] tokens = IOUtil.tokenizeString(currentConfig,
> ",");
>
>                StringBuilder builder = new StringBuilder();
>
>                for (int i = 0; i < tokens.length; i++)
>
>                {
>
>                    if (i == 0)
>
>                    {
>
>                                builder.append("INFO");
>
>                                continue;
>
>                    }
>
>                    builder.append(", " + tokens[i]);
>
>                }
>
>                return builder.toString();
>
>    }
>
>
>
>
>
>
>
>



---------------------------------------------------------------------
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



RE: Changing log4j configuration runtime ...

Posted by Preetam Palwe <pr...@aftek.com>.
Or in other words 
I want to do 

PropertyConfigurator.reconfigure(Properties prop);

Is there any way to do this ?



-----Original Message-----
From: Preetam Palwe [mailto:preetamp@aftek.com] 
Sent: Monday, June 22, 2009 12:13 PM
To: 'Log4J Users List'
Subject: RE: Changing log4j configuration runtime ...

Thanks Yair for the reply!

I have used property file only. 
The thing is the property file is open and if someone edit it and restart the java process he would be able to see all the DEBUG logs (which contains some critical information)

I dont want this to happen so want to change at runtime (in code) the configuration!


-----Original Message-----
From: Yair Ogen [mailto:yairogen@gmail.com] 
Sent: Sunday, June 21, 2009 2:38 PM
To: Log4J Users List
Subject: Re: Changing log4j configuration runtime ...

Why not use a regular property file?

Then when you to update the file you can load the changes automatically
using the "PropertyConfigurator.configureAndWatch" mechanism.


On Sun, Jun 21, 2009 at 11:41 AM, Preetam Palwe <pr...@aftek.com> wrote:

> Hello all,
>
>
>
> I have successfully configured log4j in my standalone java application
> using PropertyConfigurator.configure()
>
> Here is the configuration file
>
>
>
> log4j.rootLogger=DBUG, LogFile
>
>
>
> log4j.logger.com.server.core=INFO
>
> log4j.logger. com.server.core.SecurityManager =DEBUG
>
> log4j.logger. com.server.core.SecurityPatternsCreator =DEBUG
>
>
>
>
> log4j.appender.LogFile=org.apache.log4j.RollingFileAppender
>
>                log4j.appender.LogFile.File=c:/server.log
>
>                log4j.appender.LogFile.MaxFileSize=5MB
>
>                log4j.appender.LogFile.MaxBackupIndex=10
>
>
> log4j.appender.LogFile.layout=org.apache.log4j.PatternLayout
>
>                log4j.appender.LogFile.layout.ConversionPattern=%d{yyyy
> MM dd HH:mm:ss:SSS}# [%-5p]# [%t]# %c# %C{1}.%M# %m%n
>
>
>
> In case of my application I have a security related library
> (securitylib.jar) in which there we do some is a critical security
> related processing. And I want to set logging level of "INFO" to all the
> classes/loggers from this jar.
>
> The log4j configuration is done before loading securitylib.jar using
> above configuration file.
>
> When securitylib.jar is loaded I tried to re-configure log4j using
> following code.
>
> (what I have done here is removed all the explicit loggers set in
> configuration file, updated the configuration properties and configured
> logger again)
>
>
>
> But when I test the system I am still able to see the DEBUG logs for
> classes/loggers com.server.core.SecurityManager and
> com.server.core.SecurityPatternsCreator (which I had marked as DEBUG in
> config files)
>
>
>
> My questions are ...
>
> 1.       Why after reconfiguring the loggers the new configuration is
> not getting reflected ?
>
> 2.       Is there any way to get the current log4j configuration and
> change it at runtime ?
>
>
>
> Thanks in advance for your help!
>
> ~PP
>
>
>
> private void reinitLog4j()
>
>    {
>
>                try
>
>                {
>
>                    String log4jFile = System.getProperty("log4j.file");
>
>                    Properties properties = new Properties();
>
>                    properties.load(new FileInputStream(log4jFile));
>
>                    updateLoggingConfiguration(properties);
>
>                    PropertyConfigurator.configure(properties);
>
>                }
>
>                catch (IOException ioEx)
>
>                {
>
>                    throw new RuntimeException("failed to reconfigure
> logger "
>
>                                    + ioEx.getMessage(), ioEx);
>
>                }
>
>                logger = Logger.getLogger(this.getClass());
>
>                logger.info("Log4j RE initialised to use INFO logs
> everywhere !");
>
>                logger.debug("THIS SHOULD NEVER COME IN LOGS");
>
>    }
>
>
>
>    private void updateLoggingConfiguration(Properties properties)
>
>    {
>
>                for (Iterator iterator =
> properties.entrySet().iterator(); iterator
>
>                                .hasNext();)
>
>                {
>
>                    Entry entry = (Entry) iterator.next();
>
>                    String key = (String) entry.getKey();
>
>                    if (key.equalsIgnoreCase("log4j.rootLogger"))
>
>                    {
>
>
> entry.setValue(getRootLoggerConfiguration((String) entry
>
>                                                .getValue()));
>
>                    }
>
>                    else if (key.startsWith("log4j.logger"))
>
>                    {
>
>                                iterator.remove();
>
>                    }
>
>                }
>
>    }
>
>
>
>    private String getRootLoggerConfiguration(String currentConfig)
>
>    {
>
>                String[] tokens = IOUtil.tokenizeString(currentConfig,
> ",");
>
>                StringBuilder builder = new StringBuilder();
>
>                for (int i = 0; i < tokens.length; i++)
>
>                {
>
>                    if (i == 0)
>
>                    {
>
>                                builder.append("INFO");
>
>                                continue;
>
>                    }
>
>                    builder.append(", " + tokens[i]);
>
>                }
>
>                return builder.toString();
>
>    }
>
>
>
>
>
>
>
>



---------------------------------------------------------------------
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


RE: Changing log4j configuration runtime ...

Posted by Preetam Palwe <pr...@aftek.com>.
Thanks Yair for the reply!

I have used property file only. 
The thing is the property file is open and if someone edit it and restart the java process he would be able to see all the DEBUG logs (which contains some critical information)

I don’t want this to happen so want to change at runtime (in code) the configuration!


-----Original Message-----
From: Yair Ogen [mailto:yairogen@gmail.com] 
Sent: Sunday, June 21, 2009 2:38 PM
To: Log4J Users List
Subject: Re: Changing log4j configuration runtime ...

Why not use a regular property file?

Then when you to update the file you can load the changes automatically
using the "PropertyConfigurator.configureAndWatch" mechanism.


On Sun, Jun 21, 2009 at 11:41 AM, Preetam Palwe <pr...@aftek.com> wrote:

> Hello all,
>
>
>
> I have successfully configured log4j in my standalone java application
> using PropertyConfigurator.configure()
>
> Here is the configuration file
>
>
>
> log4j.rootLogger=DBUG, LogFile
>
>
>
> log4j.logger.com.server.core=INFO
>
> log4j.logger. com.server.core.SecurityManager =DEBUG
>
> log4j.logger. com.server.core.SecurityPatternsCreator =DEBUG
>
>
>
>
> log4j.appender.LogFile=org.apache.log4j.RollingFileAppender
>
>                log4j.appender.LogFile.File=c:/server.log
>
>                log4j.appender.LogFile.MaxFileSize=5MB
>
>                log4j.appender.LogFile.MaxBackupIndex=10
>
>
> log4j.appender.LogFile.layout=org.apache.log4j.PatternLayout
>
>                log4j.appender.LogFile.layout.ConversionPattern=%d{yyyy
> MM dd HH:mm:ss:SSS}# [%-5p]# [%t]# %c# %C{1}.%M# %m%n
>
>
>
> In case of my application I have a security related library
> (securitylib.jar) in which there we do some is a critical security
> related processing. And I want to set logging level of "INFO" to all the
> classes/loggers from this jar.
>
> The log4j configuration is done before loading securitylib.jar using
> above configuration file.
>
> When securitylib.jar is loaded I tried to re-configure log4j using
> following code.
>
> (what I have done here is removed all the explicit loggers set in
> configuration file, updated the configuration properties and configured
> logger again)
>
>
>
> But when I test the system I am still able to see the DEBUG logs for
> classes/loggers com.server.core.SecurityManager and
> com.server.core.SecurityPatternsCreator (which I had marked as DEBUG in
> config files)
>
>
>
> My questions are ...
>
> 1.       Why after reconfiguring the loggers the new configuration is
> not getting reflected ?
>
> 2.       Is there any way to get the current log4j configuration and
> change it at runtime ?
>
>
>
> Thanks in advance for your help!
>
> ~PP
>
>
>
> private void reinitLog4j()
>
>    {
>
>                try
>
>                {
>
>                    String log4jFile = System.getProperty("log4j.file");
>
>                    Properties properties = new Properties();
>
>                    properties.load(new FileInputStream(log4jFile));
>
>                    updateLoggingConfiguration(properties);
>
>                    PropertyConfigurator.configure(properties);
>
>                }
>
>                catch (IOException ioEx)
>
>                {
>
>                    throw new RuntimeException("failed to reconfigure
> logger "
>
>                                    + ioEx.getMessage(), ioEx);
>
>                }
>
>                logger = Logger.getLogger(this.getClass());
>
>                logger.info("Log4j RE initialised to use INFO logs
> everywhere !");
>
>                logger.debug("THIS SHOULD NEVER COME IN LOGS");
>
>    }
>
>
>
>    private void updateLoggingConfiguration(Properties properties)
>
>    {
>
>                for (Iterator iterator =
> properties.entrySet().iterator(); iterator
>
>                                .hasNext();)
>
>                {
>
>                    Entry entry = (Entry) iterator.next();
>
>                    String key = (String) entry.getKey();
>
>                    if (key.equalsIgnoreCase("log4j.rootLogger"))
>
>                    {
>
>
> entry.setValue(getRootLoggerConfiguration((String) entry
>
>                                                .getValue()));
>
>                    }
>
>                    else if (key.startsWith("log4j.logger"))
>
>                    {
>
>                                iterator.remove();
>
>                    }
>
>                }
>
>    }
>
>
>
>    private String getRootLoggerConfiguration(String currentConfig)
>
>    {
>
>                String[] tokens = IOUtil.tokenizeString(currentConfig,
> ",");
>
>                StringBuilder builder = new StringBuilder();
>
>                for (int i = 0; i < tokens.length; i++)
>
>                {
>
>                    if (i == 0)
>
>                    {
>
>                                builder.append("INFO");
>
>                                continue;
>
>                    }
>
>                    builder.append(", " + tokens[i]);
>
>                }
>
>                return builder.toString();
>
>    }
>
>
>
>
>
>
>
>



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


Re: Changing log4j configuration runtime ...

Posted by Yair Ogen <ya...@gmail.com>.
Why not use a regular property file?

Then when you to update the file you can load the changes automatically
using the "PropertyConfigurator.configureAndWatch" mechanism.


On Sun, Jun 21, 2009 at 11:41 AM, Preetam Palwe <pr...@aftek.com> wrote:

> Hello all,
>
>
>
> I have successfully configured log4j in my standalone java application
> using PropertyConfigurator.configure()
>
> Here is the configuration file
>
>
>
> log4j.rootLogger=DBUG, LogFile
>
>
>
> log4j.logger.com.server.core=INFO
>
> log4j.logger. com.server.core.SecurityManager =DEBUG
>
> log4j.logger. com.server.core.SecurityPatternsCreator =DEBUG
>
>
>
>
> log4j.appender.LogFile=org.apache.log4j.RollingFileAppender
>
>                log4j.appender.LogFile.File=c:/server.log
>
>                log4j.appender.LogFile.MaxFileSize=5MB
>
>                log4j.appender.LogFile.MaxBackupIndex=10
>
>
> log4j.appender.LogFile.layout=org.apache.log4j.PatternLayout
>
>                log4j.appender.LogFile.layout.ConversionPattern=%d{yyyy
> MM dd HH:mm:ss:SSS}# [%-5p]# [%t]# %c# %C{1}.%M# %m%n
>
>
>
> In case of my application I have a security related library
> (securitylib.jar) in which there we do some is a critical security
> related processing. And I want to set logging level of "INFO" to all the
> classes/loggers from this jar.
>
> The log4j configuration is done before loading securitylib.jar using
> above configuration file.
>
> When securitylib.jar is loaded I tried to re-configure log4j using
> following code.
>
> (what I have done here is removed all the explicit loggers set in
> configuration file, updated the configuration properties and configured
> logger again)
>
>
>
> But when I test the system I am still able to see the DEBUG logs for
> classes/loggers com.server.core.SecurityManager and
> com.server.core.SecurityPatternsCreator (which I had marked as DEBUG in
> config files)
>
>
>
> My questions are ...
>
> 1.       Why after reconfiguring the loggers the new configuration is
> not getting reflected ?
>
> 2.       Is there any way to get the current log4j configuration and
> change it at runtime ?
>
>
>
> Thanks in advance for your help!
>
> ~PP
>
>
>
> private void reinitLog4j()
>
>    {
>
>                try
>
>                {
>
>                    String log4jFile = System.getProperty("log4j.file");
>
>                    Properties properties = new Properties();
>
>                    properties.load(new FileInputStream(log4jFile));
>
>                    updateLoggingConfiguration(properties);
>
>                    PropertyConfigurator.configure(properties);
>
>                }
>
>                catch (IOException ioEx)
>
>                {
>
>                    throw new RuntimeException("failed to reconfigure
> logger "
>
>                                    + ioEx.getMessage(), ioEx);
>
>                }
>
>                logger = Logger.getLogger(this.getClass());
>
>                logger.info("Log4j RE initialised to use INFO logs
> everywhere !");
>
>                logger.debug("THIS SHOULD NEVER COME IN LOGS");
>
>    }
>
>
>
>    private void updateLoggingConfiguration(Properties properties)
>
>    {
>
>                for (Iterator iterator =
> properties.entrySet().iterator(); iterator
>
>                                .hasNext();)
>
>                {
>
>                    Entry entry = (Entry) iterator.next();
>
>                    String key = (String) entry.getKey();
>
>                    if (key.equalsIgnoreCase("log4j.rootLogger"))
>
>                    {
>
>
> entry.setValue(getRootLoggerConfiguration((String) entry
>
>                                                .getValue()));
>
>                    }
>
>                    else if (key.startsWith("log4j.logger"))
>
>                    {
>
>                                iterator.remove();
>
>                    }
>
>                }
>
>    }
>
>
>
>    private String getRootLoggerConfiguration(String currentConfig)
>
>    {
>
>                String[] tokens = IOUtil.tokenizeString(currentConfig,
> ",");
>
>                StringBuilder builder = new StringBuilder();
>
>                for (int i = 0; i < tokens.length; i++)
>
>                {
>
>                    if (i == 0)
>
>                    {
>
>                                builder.append("INFO");
>
>                                continue;
>
>                    }
>
>                    builder.append(", " + tokens[i]);
>
>                }
>
>                return builder.toString();
>
>    }
>
>
>
>
>
>
>
>