You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Piotr Karwasz (Jira)" <ji...@apache.org> on 2023/01/03 08:13:00 UTC
[jira] [Commented] (LOG4J2-3646) Log4jBridgeHandler does not work OTB on Tomcat 10.1
[ https://issues.apache.org/jira/browse/LOG4J2-3646?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17653853#comment-17653853 ]
Piotr Karwasz commented on LOG4J2-3646:
---------------------------------------
[~matthewfirth],
JUL has no concept of logger context. Tomcat solves this by making {{Logger.getLogger}} a context-sensitive method.
If your application has no {{logging.properties}} file in the webapp classloader, Tomcat attaches the root logger of your application's "logger context" to the root logger of the "global logger context". I don't see anything wrong in this (the main problem is JUL).
I agree that calling `setUseParentHandlers(false)` (even unconditionally) is the right thing to do. Can you send a PR on Github with your fix and a unit test? You can find a similar unit test in {{slf4j-to-jul}}: {{org.apache.logging.log4j.tojul.JULLoggerTest}}
> Log4jBridgeHandler does not work OTB on Tomcat 10.1
> ---------------------------------------------------
>
> Key: LOG4J2-3646
> URL: https://issues.apache.org/jira/browse/LOG4J2-3646
> Project: Log4j 2
> Issue Type: Bug
> Components: JUL adapter
> Affects Versions: 2.19.0
> Reporter: Matthew Firth
> Priority: Minor
>
> Related to the issue I filed in the Tomcat Bugzilla: [https://bz.apache.org/bugzilla/show_bug.cgi?id=66406]
> The Log4jBridgeHandler's logic to clear existing handlers on the root logger doesn't work in TC 10 and possibly other situations. The result is that I get all log messages published twice - once to the console by JUL and then once again by log4j.
> Log4jBridgeHandler.getJulRootLogger() assumes that the "root logger" is the one returned by getLogger("").
> When running TC10 with JULI (the default), that Logger actually has two parents - and the grand-parent Logger has the Console Handler attached to it.
> Even if the Tomcat Project doesn't change JULI, I suggest log4j could be defensive and simply block parent loggers if any exist.
> For example - ammending Log4jBridgeHandler.install() like this:
> {noformat}
> if (removeHandlersForRootLogger) {
> for (java.util.logging.Handler hdl : rootLogger.getHandlers()) {
> rootLogger.removeHandler(hdl);
> }
>
> if (rootLogger.getParent() != null)
> rootLogger.setUseParentHandlers(false);
> rootLogger.addHandler(new Log4jBridgeHandler(false, suffixToAppend, propagateLevels));
> }
>
> {noformat}
>
>
> )
--
This message was sent by Atlassian Jira
(v8.20.10#820010)