logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tarun Sharma <tarun.k.sha...@oracle.com>
Subject RE: log4j2 - Logger does not have the configuration If procured before LoggerContext has initialized
Date Wed, 30 Nov 2016 08:47:26 GMT
Hi Mikael,

The product I am working on holds about 15 odd configurations which are specific to different
process that run as part of the application. It is a core java app.
So, every time a process starts the configuration is loaded on the fly.
We store configurations in a properties file with a set of configurations for a process with
a prefix "config_proc1" for 'proc1' process.

This is the reason why I have taken an example which loads configurations programmatically.

So, we have 'p' object(in the snippet) is a subset of properties read from a properties file.


Regards,
Tarun


-----Original Message-----
From: Mikael Ståldal [mailto:mikael.staldal@magine.com] 
Sent: Wednesday, November 30, 2016 2:11 PM
To: Log4J Users List
Subject: Re: log4j2 - Logger does not have the configuration If procured before LoggerContext
has initialized

Any particular reason for not using a configuration file?

On Wed, Nov 30, 2016 at 5:31 AM, Tarun Sharma <tarun.k.sharma@oracle.com>
wrote:

> Hi,
>
>
>
> While upgrading from 1.2.17 to 2.5, I have come across a major 
> difference.  Please see following code snippets.
>
>
>
> Log4j1.2.17
>
> private  static final Logger LOGGER = Logger.getLogger(TestLog.class);
>
> public static void main(String args[]) {
>     Properties p = new Properties();
>     p.setProperty("log4j.debug","true");
>     p.setProperty("log4j.rootLogger","info,cfg");
>     p.setProperty("log4j.appender.cfg","org.apache.log4j.
> ConsoleAppender");
>     p.setProperty("log4j.appender.cfg.layout","org.apache.log4j.
> PatternLayout");
>     PropertyConfigurator.configure(p);
>     LOGGER.info("INFO"); //logs on console
>     LOGGER.warn("warn");  //logs on console
>     LOGGER.error("error");  //logs on console }
>
>
>
> Log4j2 v2.5
>
>
>
> private static final Logger LOGGER = 
> LogManager.getLogger(TestLog.class);
>
>     public static void main(String[] args)
>     {
>         PropertiesConfigurationFactory factory = new
> PropertiesConfigurationFactory();   // This line and the try catch below
> replace the
>         Properties cfg = new Properties();
>         cfg.setProperty("name", "config");
>         cfg.setProperty("status", "debug");
>         cfg.setProperty("appenders","console");
>         cfg.setProperty("appender.console.type","Console");
>         cfg.setProperty("appender.console.name","Console");
>         cfg.setProperty("appender.console.layout.type","PatternLayout");
>         cfg.setProperty("rootLogger.level","info");
>         cfg.setProperty("rootLogger.appenderRefs","console");
>         cfg.setProperty("rootLogger.appenderRef.console.ref","Console");
>         try {
>             ConfigurationSource configSrc = createConfigurationSource(cfg);
>               //PropertyConfigurator.configure(cfg); from log4j1.2
>
>             Configuration conf = factory.getConfiguration(configSrc);
>             System.setProperty("Log4jContextSelector",
> "org.apache.logging.log4j.core.selector.BasicContextSelector");
>             LoggerContext ctx = Configurator.initialize(conf);
>             ContextAnchor.THREAD_CONTEXT.set(ctx);
>         }
>         catch (IOException io)
>         {
>
>         }
>
>         LOGGER.info("INFO");  // does not print
>         LOGGER.warn("warn");    // does not print
>         LOGGER.error("error");  //prints to console
>     }
>
>     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);
>     }
> }
>
>
>
>
>
> So, In log4j2 , the Logger which is initialized as private static 
> final, is not aware of the configurations, but in logj1.x, It is.
>
>
>
> Is there any way to make log4j2 code behave as log4j1.x did?
>
>
>
> Best Regards,
>
> Tarun
>



--
[image: MagineTV]

*Mikael Ståldal*
Senior software developer

*Magine TV*
mikael.staldal@magine.com
Grev Turegatan 3  | 114 46 Stockholm, Sweden  |   www.magine.com

Privileged and/or Confidential Information may be contained in this message. If you are not
the addressee indicated in this message (or responsible for delivery of the message to such
a person), you may not copy or deliver this message to anyone. In such case, you should destroy
this message and kindly notify the sender by reply email.

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


Mime
View raw message