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

Carter Kozak reassigned LOG4J2-2945:
------------------------------------

    Assignee: Carter Kozak

> Better validation and messaging for classpath issues
> ----------------------------------------------------
>
>                 Key: LOG4J2-2945
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-2945
>             Project: Log4j 2
>          Issue Type: Improvement
>          Components: Boot, Core
>    Affects Versions: 2.13.3
>            Reporter: Michiel Graat
>            Assignee: Carter Kozak
>            Priority: Minor
>
> Hi,
> [~ckozak] suggested I created an issue for this on the [mailing 
> list|https://lists.apache.org/thread.html/r1de0b24e43b126bc6c05c1b092b18c8bbb3c1418a289c46cb485aa0f%40%3Clog4j-user.logging.apache.org%3E].
>  While migrating from Log4J 1.x to Log4J2 I accidently left a stray Log4J 1.x 
> JAR in my application artifact (and thus on the classpath). This lead to some 
> strange behavior: the logging to the root appender worked, but the other 
> loggers I defined where ignored (that is, the files were created but they 
> remained empty). 
> It was never clear there was a classpath issue, even the Log4J2 trace logging 
> gave no further information.
> So this is a request to improve the validation and messaging on this issue.
> Carter suggested the following as a starting point:
> {quote}I've written similar validation for slf4j bindings elsewhere that we 
> may be able to leverage (though the general case has many edge cases that I 
> haven't thought through yet):
> {code}/**
> * Validate that there is only one slf4j binding present. If there are 
> multiple distinct bindings logging will
> * disappear into the void depending on classpath order.
> */
> private static void validateSlf4jBindings() {
>     try {
>         List<URL> slf4jBindings = Collections.list(
>                 
> getClassLoader().getResources("org/slf4j/impl/StaticLoggerBinder.class"));
>         if (slf4jBindings.size() > 1) {
>             String message = "Detected multiple Slf4j bindings: " + 
> slf4jBindings;
>             // Write to standard streams, if the exception is caught and 
> logged, it may not be recorded.
>             System.err.println(message);
>             throw new IllegalStateException(message);
>         }
>     } catch (IOException e) {
>         throw new IllegalStateException("Failed to discover slf4j bindings", 
> e);
>     }
> }
> {code}
> {quote}
> Cheers,
> Michiel



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

Reply via email to