Return-Path: Delivered-To: apmail-logging-log4j-user-archive@www.apache.org Received: (qmail 62778 invoked from network); 21 Jun 2009 09:08:24 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 21 Jun 2009 09:08:24 -0000 Received: (qmail 28879 invoked by uid 500); 21 Jun 2009 09:08:34 -0000 Delivered-To: apmail-logging-log4j-user-archive@logging.apache.org Received: (qmail 28798 invoked by uid 500); 21 Jun 2009 09:08:34 -0000 Mailing-List: contact log4j-user-help@logging.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Log4J Users List" Reply-To: "Log4J Users List" Delivered-To: mailing list log4j-user@logging.apache.org Received: (qmail 28787 invoked by uid 99); 21 Jun 2009 09:08:34 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 21 Jun 2009 09:08:34 +0000 X-ASF-Spam-Status: No, hits=2.2 required=10.0 tests=HTML_MESSAGE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of yairogen@gmail.com designates 209.85.220.211 as permitted sender) Received: from [209.85.220.211] (HELO mail-fx0-f211.google.com) (209.85.220.211) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 21 Jun 2009 09:08:21 +0000 Received: by fxm7 with SMTP id 7so2630738fxm.22 for ; Sun, 21 Jun 2009 02:08:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :from:date:message-id:subject:to:content-type; bh=eH/FUz739MBegeA+C1RHMyyli7xb1VMvMdg4f+a+B/Q=; b=T9SF4bhkc8l0yTNrPzgsIkQZQJo6rTOfYeJpsQckuz/3PFpk+yW2NoG9Z+/J+sGE2g Nez3d8+nv03WZVrp/5VAFUsE5AhVOc/WSsnLcagE2OXqvO/rK/y8GzMOccrskZ2XYsmQ rd6C4B/yZ02S8tdsoCvSQkol/Yp8ARnUapPdw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; b=KFsmHtS3QWrw4EQofFWEHQLmWQw07/1tNUzr2zwc/6vmrA8BOAmJ2aRg6SBRS0q1Fc vQ1IWOzOzjc0eJIMrDxLGfzax2nDuhTSuF9REKlFQixPrTsEiPaVTecRDSIzfyrt3WaT e3JWfXGpkOPBQLuGnEjXQkuAlbnVrE5s9oNfs= MIME-Version: 1.0 Received: by 10.223.126.1 with SMTP id a1mr4205518fas.78.1245575281144; Sun, 21 Jun 2009 02:08:01 -0700 (PDT) In-Reply-To: References: From: Yair Ogen Date: Sun, 21 Jun 2009 12:07:41 +0300 Message-ID: Subject: Re: Changing log4j configuration runtime ... To: Log4J Users List Content-Type: multipart/alternative; boundary=001636c5a5372c7414046cd81801 X-Virus-Checked: Checked by ClamAV on apache.org --001636c5a5372c7414046cd81801 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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 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(); > > } > > > > > > > > --001636c5a5372c7414046cd81801--