Remko, since he is using Configurator he shouldn’t have to call updateLoggers. 
However, he is calling System.setProperty to set the LoggerContext 
implementation to use AFTER the static Logger is created. There is no hope that 
could ever work. The best way to fix that would be to add the setting to 
log4j2.component.properties.

It would also be much better to prepare the configuration before initializing 
the Logger just to avoid initializing Log4j twice.

Also, I’m not sure I understand the reason for programmatic configuration. It 
would be easier to just call Configurator.initialize and pass it the name of a 
file where the file name is specific to the app. Better yet, if the 
configurations are similar just use properties within the log4j configuration 
to tailor them to each application.

Ralph

> On Nov 30, 2016, at 3:03 AM, Remko Popma <remko.po...@gmail.com> wrote:
> 
> 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.sha...@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.stal...@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.sha...@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.stal...@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-unsubscr...@logging.apache.org
>> For additional commands, e-mail: log4j-user-h...@logging.apache.org
>> 



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

Reply via email to