You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Caleb Cushing (Jira)" <ji...@apache.org> on 2021/03/01 18:09:00 UTC

[jira] [Updated] (LOG4J2-3031) Doc/Bug?/Feature: code configuration better

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

Caleb Cushing updated LOG4J2-3031:
----------------------------------
    Description: 
I've had nothing but problems trying to get the code configuration working. I hate the `ConfigurationBuilder` API, it's easier to get wrong, but I couldn't get the "Modifying the Current Configuration after Initialization" API working. I wish that one would support all the same initial configuration use case (feature, I can write a separate ticket if you want).

First, there's this question https://stackoverflow.com/q/65194471/206466

```java
private static void configureLog4j(Level rootLevel, Map<String, Level> levelMap) {
    var pattern = PatternLayout.newBuilder().withPattern( "%highlight{[%t] %-5level: %msg%n%throwable}\n" ).build();
    var console = ConsoleAppender.createDefaultAppenderForLayout( pattern );
    var config = LoggerContext.getContext().getConfiguration();
    config.addAppender( console );

    var root = config.getRootLogger();
    for ( var appenderRef : root.getAppenderRefs() ) {
      root.removeAppender( appenderRef.getRef() );
    }
    root.addAppender( console, rootLevel, null );
    Configurator.setRootLevel( rootLevel );
    Configurator.setLevel( levelMap );
    Configurator.reconfigure(config);
  }
```

ended up using the other API and it worked, though I would much rather use this one, as it doesn't require stringy representations.

Next, I tried setting up a `patternSelector` for the root appender in another project using the `ConfigurationBuilder`, I had no luck figuring that out, it simply seemed not to use what I put in. Is this maybe a bug? I can add code for what I tried here later.

P.S. I want you to know about [this bug](https://github.com/gradle/gradle/issues/16366), but I doubt it belongs to log4j, but maybe you know something. 



     Issue Type: Improvement  (was: Bug)
        Summary: Doc/Bug?/Feature: code configuration better  (was: code configuration woes)

> Doc/Bug?/Feature: code configuration better
> -------------------------------------------
>
>                 Key: LOG4J2-3031
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-3031
>             Project: Log4j 2
>          Issue Type: Improvement
>            Reporter: Caleb Cushing
>            Priority: Major
>
> I've had nothing but problems trying to get the code configuration working. I hate the `ConfigurationBuilder` API, it's easier to get wrong, but I couldn't get the "Modifying the Current Configuration after Initialization" API working. I wish that one would support all the same initial configuration use case (feature, I can write a separate ticket if you want).
> First, there's this question https://stackoverflow.com/q/65194471/206466
> ```java
> private static void configureLog4j(Level rootLevel, Map<String, Level> levelMap) {
>     var pattern = PatternLayout.newBuilder().withPattern( "%highlight{[%t] %-5level: %msg%n%throwable}\n" ).build();
>     var console = ConsoleAppender.createDefaultAppenderForLayout( pattern );
>     var config = LoggerContext.getContext().getConfiguration();
>     config.addAppender( console );
>     var root = config.getRootLogger();
>     for ( var appenderRef : root.getAppenderRefs() ) {
>       root.removeAppender( appenderRef.getRef() );
>     }
>     root.addAppender( console, rootLevel, null );
>     Configurator.setRootLevel( rootLevel );
>     Configurator.setLevel( levelMap );
>     Configurator.reconfigure(config);
>   }
> ```
> ended up using the other API and it worked, though I would much rather use this one, as it doesn't require stringy representations.
> Next, I tried setting up a `patternSelector` for the root appender in another project using the `ConfigurationBuilder`, I had no luck figuring that out, it simply seemed not to use what I put in. Is this maybe a bug? I can add code for what I tried here later.
> P.S. I want you to know about [this bug](https://github.com/gradle/gradle/issues/16366), but I doubt it belongs to log4j, but maybe you know something. 



--
This message was sent by Atlassian Jira
(v8.3.4#803005)