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

[jira] [Updated] (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:all-tabpanel ]

Matthew Firth updated LOG4J2-3646:
----------------------------------
    Description: 
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}
 
 

) 

  was:
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}
 
 

) 


> 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)