Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id DC366200BC7 for ; Fri, 25 Nov 2016 07:34:16 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id DAD21160B01; Fri, 25 Nov 2016 06:34:16 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 2D2E4160AF6 for ; Fri, 25 Nov 2016 07:34:16 +0100 (CET) Received: (qmail 53178 invoked by uid 500); 25 Nov 2016 06:34:15 -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 53166 invoked by uid 99); 25 Nov 2016 06:34:15 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 25 Nov 2016 06:34:15 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 93BABD3CEF for ; Fri, 25 Nov 2016 06:34:14 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.88 X-Spam-Level: * X-Spam-Status: No, score=1.88 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HTML_MESSAGE=2, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=disabled Authentication-Results: spamd1-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id hFTLtMwUqzVC for ; Fri, 25 Nov 2016 06:34:11 +0000 (UTC) Received: from mail-oi0-f52.google.com (mail-oi0-f52.google.com [209.85.218.52]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTPS id 7C57F5FAED for ; Fri, 25 Nov 2016 06:34:10 +0000 (UTC) Received: by mail-oi0-f52.google.com with SMTP id w63so72502628oiw.0 for ; Thu, 24 Nov 2016 22:34:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=R6625UhtAQ1m1M6W9Jb3o9sx+q6UlDrbbsoVJcezPr0=; b=ss4Dxo+h+9h6bM+bF2olVH/ut0aIEJkVOysDB7wgixYuhog0EToKPLWhj/EwZ09vti JhuF/pvS/vFVKo9YC7xigCqUBIJAgzdYWItU3CR1MUubVdHGCUxhXLVhNMakDpeFs5Wb tHTUJNsAtHZ5ikczctjlrMYZ0inSYmUcAsyKl+ULLPhdXlIp7SFZ/FkO0IFKV76wANmx kyNWv83TmdKkQ5WvW4hU21C3U04mXr9sskzgEs/C0ABQMwAewxd56oZJJRJZKtVMHX/U utphE0BYkO7NlePDD5vYeYjkkEGwM6QH65Ot3sbJluIGYm6YChrcI6chqiK2cIM74aC8 70Qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=R6625UhtAQ1m1M6W9Jb3o9sx+q6UlDrbbsoVJcezPr0=; b=dBlCy7f/N1u5iCTyBl6foJK5dRVb6frMfAZn8v9iS+NDw1tf+M1GO6JYzeIx86ABxD tQ9v5OIeQQrPf5DCmOgjzpVGjbpemuH2tJfzFurQTWRVMSplDCajCYftHGPpPHDSXJ3q yQ+o/QH6L3Q3EYEIHg/93YcOdj/AKeOc5Fxh8sJhtCb1lPzsEUkqRv9Z0Hdd4wjCc4KK 0lB9KgScSmKW+m9UldBibxgORcoTL4JGN+pgw4T4oZWe2P082ihs3/43+mKK+d255NkH gwZXxHwbdLMM7yjCJ9YBJEivd70Gsrf9NhATq5Mqn8NwXXORaHdhgXI2iyYTw/6m5XH1 CWBw== X-Gm-Message-State: AKaTC01pDjw/5LNx5W88xTk8tDUQQoqFzpog+sCCtnWSPSR3zCq1Oj1ryt+nZIJecY0rVozHIofd0p7sVMdWAQ== X-Received: by 10.157.54.245 with SMTP id s50mr3501886otd.181.1480055648937; Thu, 24 Nov 2016 22:34:08 -0800 (PST) MIME-Version: 1.0 Received: by 10.157.35.40 with HTTP; Thu, 24 Nov 2016 22:34:08 -0800 (PST) Received: by 10.157.35.40 with HTTP; Thu, 24 Nov 2016 22:34:08 -0800 (PST) In-Reply-To: References: From: Gary Gregory Date: Thu, 24 Nov 2016 22:34:08 -0800 Message-ID: Subject: Re: Configuring log4j2 using a dynamic changing properties read from a properties file To: Log4J Users List Content-Type: multipart/alternative; boundary=001a113b080233ee1505421a4f42 archived-at: Fri, 25 Nov 2016 06:34:17 -0000 --001a113b080233ee1505421a4f42 Content-Type: text/plain; charset=UTF-8 Quick note: why not 2.7? Gary On Nov 24, 2016 9:57 PM, "Tarun Sharma" wrote: > Hello All, > > > > I am upgrading a 1.x version log4j product to 2.5 > > > > We have a single properties file which holds various configurations. A > sample configuration(trace.properties) is as below: > > > > cfg_test.status=debug > cfg_test.appenders=rolling > cfg_test.appender.rolling.type=RollingFile > cfg_test.appender.rolling.name=RollingFile_TEST > cfg_test.appender.rolling.fileName=D:/log/test.log > cfg_test.appender.rolling.filePattern=D:/log/test_%d{MMdd}.log > cfg_test.appender.rolling.layout.type=PatternLayout > cfg_test.appender.rolling.layout.pattern=%d %-5p %m%n > cfg_test.appender.rolling.policies.type = Policies > cfg_test.appender.rolling.policies.time.type = TimeBasedTriggeringPolicy > cfg_test.appender.rolling.policies.time.interval = 1 > cfg_test.appender.rolling.policies.time.modulate = true > cfg_test.appender.rolling.policies.size.type = SizeBasedTriggeringPolicy > cfg_test.appender.rolling.policies.size.size=100MB > cfg_test.appender.rolling.strategy.type = DefaultRolloverStrategy > cfg_test.appender.rolling.strategy.max = 5 > > cfg_test.rootLogger.level=debug > cfg_test.rootLogger.appenderRefs=test > cfg_test.rootLogger.appenderRef.test.ref=RollingFile_TEST > > > > cfg_crp.rootLogger.level=info > cfg_crp.rootLogger.appenderRefs=crp, stdout > cfg_crp.rootLogger.appenderRef.crp.ref=RollingFile_CRP > cfg_crp.rootLogger.appenderRef.stdout.ref=STDOUT > > > > > > Here cfg_test is a test configuration and cfg_crp is a configuration for a > functionality in product which is launched as a separate module. > > > > I have a custom factory as below: > > > > public class TraceFactory { > > private static boolean isInitialized = false; > > > /** > * intializes the trace with the given trace key. > *

> * If if the trace key is null, we will use the console trace > * > * @param traceKey trace key > */ > public static void initialize(String traceKey) { > initPrefix(traceKey != null ? "cfg_" + traceKey + "." : null, > PropertiesUtils.readFromClassPath("properties/trace")); > } > > private static void initPrefix(String prefix, Properties properties) { > if (isInitialized) { > return; > } > > if (prefix == null) { > TraceFactory.initializeAsConsoleTracer(); > return; > } > > Properties cfg = new Properties(); > > for (Map.Entry objectObjectEntry : > properties.entrySet()) { > Map.Entry entry = (Map.Entry) objectObjectEntry; > String key = (String) entry.getKey(); > String value = (String) entry.getValue(); > > if (key.startsWith(prefix)) { > cfg.put(key.substring(prefix.length()), value); > } > } > > PropertiesConfigurationFactory factory = new > PropertiesConfigurationFactory(); // This line and the try catch below > replace the > try { > ConfigurationSource configSrc = createConfigurationSource(cfg); > //PropertyConfigurator.configure(cfg); from log4j1.2 > > Configuration conf = factory.getConfiguration(configSrc); > LoggerContext ctx = Configurator.initialize(conf); > > ctx.reconfigure(); > } > catch (IOException io) > { > > } > > isInitialized = true; > } > > /** > * > * @param cfg the log4j configuration as a properties bundle read from a > properties file. > * @return {@link ConfigurationSource} object > * @throws IOException > */ > private static ConfigurationSource createConfigurationSource(Properties > cfg) throws IOException { > > ByteArrayOutputStream out = new ByteArrayOutputStream(); > cfg.store(out, null); > InputStream in = new ByteArrayInputStream(out.toByteArray()); > return new ConfigurationSource(in); > } > > > > public static TraceInterface getTracer(Class class_) { > if (useConsoleTracer) { > return new ConsoleTracer(null); > } > return Log4JTracer.getTracer(class_); > } > > > public static TraceInterface getTracer(String name) { > if (useConsoleTracer) { > return new ConsoleTracer(null); > } > return Log4JTracer.getTracer(name); > } > } > > My LogWrapper Log4JTracer is :- > > public class Log4JTracer implements TraceInterface { > > > private static final String FQCN = Log4JTracer.class.getName(); > > /** > * extended logger wrapper > */ > private final ExtendedLoggerWrapper log; > > > private Log4JTracer(final Logger logger) { > this.log = new ExtendedLoggerWrapper((AbstractLogger)logger, > logger.getName(), logger.getMessageFactory()); > } > > > > /** > * {@inheritDoc} > */ > public void debug(Object message) { > debug(message, null); > } > > /** > * {@inheritDoc} > */ > public void debug(Object message, Throwable t) { > log.logIfEnabled(FQCN, Level.DEBUG, null, message, t); > } > > > /** > * {@inheritDoc} > */ > public void info(Object message) { > info(message, null); > } > > /** > * {@inheritDoc} > */ > public void info(Object message, Throwable t) { > log.logIfEnabled(FQCN, Level.INFO, null, message, t); > } > > > > /** > * {@inheritDoc} > */ > public boolean isWarnEnabled() { > return log.isWarnEnabled(); > } > > > /** > * {@inheritDoc} > */ > public void warn(Object message) { > warn(message, null); > } > > /** > * {@inheritDoc} > */ > public void warn(Object message, Throwable t) { > log.logIfEnabled(FQCN, Level.WARN, null, message, t); > > > /** > * {@inheritDoc} > */ > public boolean isErrorEnabled() { > return log.isErrorEnabled(); > } > > /** > * {@inheritDoc} > */ > public void error(Object message) { > error(message, null); > } > > /** > * {@inheritDoc} > */ > public void error(Object message, Throwable t) { > log.logIfEnabled(FQCN, Level.ERROR, null, message, t); > } > > /** > * {@inheritDoc} > */ > public boolean isFatalEnabled() { > return log.isFatalEnabled(); > } > > > > > /** > * {@inheritDoc} > */ > public void fatal(Object message, Throwable t) { > log.logIfEnabled(FQCN, Level.FATAL, null, message, t); > } > > > /** > * {@inheritDoc} > */ > @Override > public boolean isDebugEnabled() { > return log.isDebugEnabled(); > } > > /** > * {@inheritDoc} > */ > @Override > public boolean isInfoEnabled() { > return log.isInfoEnabled(); > } > > > public static Log4JTracer getTracer(Class cl) { > return new Log4JTracer(cl); > } > > > public static Log4JTracer getTracer(String name) { > return new Log4JTracer(name); > } > > } > > > Now when I try to use a logger from a class as below, It does not get the > logger according to the config: > > > > Public class Test { > > > > Private TraceInterface trace; > > > > Public void execute() { > > > > TraceFactory.initialize("test"); > > trace = TraceFactory.getTracer(); > > trace.debug("testing.."); // this never prints. Although a test.log is > created with zero size as soon as the TraceFactory.initialize method > finshes. > > > > > > Thanks and Regards, > > Tarun > --001a113b080233ee1505421a4f42--