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 <[email protected]> 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(); > > } > > > > > > > >
