logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Remko Popma <remko.po...@gmail.com>
Subject Re: log4j2 - Logger does not have the configuration If procured before LoggerContext has initialized
Date Wed, 30 Nov 2016 10:03:25 GMT
The first line: private static final Logger LOGGER = LogManager.getLogger(TestLog.class);
is executed before the code in main(). This means that Log4j will initialize to the default
configuration (since it cannot find a configuration file). 

After that, your code in main() kicks in. 
It is possible to reconfigure on the fly (what you're doing in main(), but after you're done
you need to call loggerContext.updateLoggers();
for the changes to take effect. 

See for more details: https://logging.apache.org/log4j/2.x/manual/customconfig.html
(Especially section Programmatically Modifying the Current Configuration after Initialization)


Sent from my iPhone

> On 30 Nov 2016, at 17:47, Tarun Sharma <tarun.k.sharma@oracle.com> wrote:
> 
> 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
  • Unnamed multipart/alternative (inline, 7-Bit, 0 bytes)
View raw message