You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by "Simon Cooper (JIRA)" <ji...@apache.org> on 2017/01/19 12:56:26 UTC

[jira] [Updated] (LOG4J2-1794) Programmatic reconfiguration doesn't affect existing loggers

     [ https://issues.apache.org/jira/browse/LOG4J2-1794?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Simon Cooper updated LOG4J2-1794:
---------------------------------
    Description: 
Sample code:

{code}    private static final Logger Log = LogManager.getLogger();

    public static void main(String[] args) throws Throwable {
        ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
        builder.setConfigurationName("Test");
        AppenderComponentBuilder console = builder.newAppender("Stdout", "Console")
                .add(builder.newLayout("PatternLayout").addAttribute("pattern", "Format: %m%n"));
        builder.add(console);
        builder.add(builder.newRootLogger(Level.INFO).add(builder.newAppenderRef("Stdout")));
        Configurator.initialize(builder.build());
        Configurator.setRootLevel(Level.INFO);

        LogManager.getLogger().info("Log!");
    }{code}

The log statement at the bottom doesn't use the specified format, it uses the existing format. If the {{private static final Log = ...}} statement at the top is commented out, the log *does* use the reconfigured format.

Reconfiguring the log settings should affect any loggers already created. Waiting until the logging is configured before creating any loggers is not always feasible - for example, if your entrypoint class has a static {{Logger}} and the reconfiguration is being done by a library you're calling into.

  was:
Sample code:

{code}    private static final Logger Log = LogManager.getLogger();

    public static void main(String[] args) throws Throwable {
        ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
        builder.setConfigurationName("Test");
        AppenderComponentBuilder console = builder.newAppender("Stdout", "Console")
                .addAttribute("follow", true)
                .add(builder.newLayout("PatternLayout").addAttribute("pattern", "Format: %m%n"));
        builder.add(console);
        builder.add(builder.newRootLogger(Level.INFO).add(builder.newAppenderRef("Stdout")));
        Configurator.initialize(builder.build());
        Configurator.setRootLevel(Level.INFO);

        LogManager.getLogger().info("Log!");
    }{code}

The log statement at the bottom doesn't use the specified format, it uses the existing format. If the {{private static final Log = ...}} statement at the top is commented out, the log *does* use the reconfigured format.

Reconfiguring the log settings should affect any loggers already created. Waiting until the logging is configured before creating any loggers is not always feasible - for example, if your entrypoint class has a static {{Logger}} and the reconfiguration is being done by a library you're calling into.


> Programmatic reconfiguration doesn't affect existing loggers
> ------------------------------------------------------------
>
>                 Key: LOG4J2-1794
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1794
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Configurators
>    Affects Versions: 2.7
>            Reporter: Simon Cooper
>
> Sample code:
> {code}    private static final Logger Log = LogManager.getLogger();
>     public static void main(String[] args) throws Throwable {
>         ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
>         builder.setConfigurationName("Test");
>         AppenderComponentBuilder console = builder.newAppender("Stdout", "Console")
>                 .add(builder.newLayout("PatternLayout").addAttribute("pattern", "Format: %m%n"));
>         builder.add(console);
>         builder.add(builder.newRootLogger(Level.INFO).add(builder.newAppenderRef("Stdout")));
>         Configurator.initialize(builder.build());
>         Configurator.setRootLevel(Level.INFO);
>         LogManager.getLogger().info("Log!");
>     }{code}
> The log statement at the bottom doesn't use the specified format, it uses the existing format. If the {{private static final Log = ...}} statement at the top is commented out, the log *does* use the reconfigured format.
> Reconfiguring the log settings should affect any loggers already created. Waiting until the logging is configured before creating any loggers is not always feasible - for example, if your entrypoint class has a static {{Logger}} and the reconfiguration is being done by a library you're calling into.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

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