Remko Popma commented on LOG4J2-1585:

The warning can be useful for some people so I don't think we want to remove 
it. We could add an option that allows you to suppress the warning if it really 
bothers you. 

As you say at the bottom of your message, I don't think there is much point in 
Log4j 2 to get the parent Logger, unless you have configured the parent logger 
to log at a different level or to a different appender. Is that your use case?

I'm trying to understand why you need the parent logger...

> Logger.getParent gives warning about MessageFactory mismatch
> ------------------------------------------------------------
>                 Key: LOG4J2-1585
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1585
>             Project: Log4j 2
>          Issue Type: Bug
>    Affects Versions: 2.6.2
>            Reporter: Di Shang
> Hi
> I just noticed a lot of warning spam in my log like this:
> 2016-09-15 14:59:16,322 main WARN The Logger  was created with the message 
> factory org.apache.logging.log4j.message.ReusableMessageFactory@51e2adc7 and 
> is now requested with the message factory 
> org.apache.logging.log4j.message.ReusableMessageFactory@1a8a8f7c, which may 
> create log events with unexpected formatting.
> The root cause of this is in Logger.getParent() :
> {code:java}
>         final MessageFactory messageFactory = getMessageFactory();
>         if (context.hasLogger(lcName, messageFactory)) {          // <=====
>             return context.getLogger(lcName, messageFactory);   // <=====
>         }
>         return new Logger(context, lcName, messageFactory);
> {code}
> It is trying to fetch the parent logger using the child's messageFactory, 
> which will cause the warning from context.getLogger. To me this looks like a 
> bug since the parent and child can have different messageFactory instance. 
> (or are they supposed to be the same?)
> Easy way to reproduce this:
> {code:java}
>         LogManager.getRootLogger();
>         Logger logger = LogManager.getLogger("foo");
>         ((org.apache.logging.log4j.core.Logger) logger).getParent();
> {code}
> I was thinking about a fix like this:
> {code:java}
>         if (context.hasLogger(lcName)) {
>             return context.getLogger(lcName);
>         }
>         return new Logger(context, lcName, getMessageFactory());
> {code}
> But it doesn't look quite right because the LoggerRegistry seems to identify 
> unique logger by (MF_class_name, logger_name) meaning there can be multiple 
> loggers of the same name but different MF class. 
> So the real question is if the parent-child relationship is determined by the 
> name only but there are multiple loggers of the same name, how can we 
> uniquely determine the parent logger?
> Reviewing the log4j 2 architecture documentation, I realized that it only 
> mentions about parent-child relationship between LoggerConfig, not Loggers. 
> Does this mean that the parent-child hierarchy is only determined by the 
> underlying LoggerConfig and for a particular LoggerConfig there can be 
> multiple loggers of the same name but different MF. So Logger.getParent() 
> doesn't make much sense any more and should not be used. Is my understanding 
> here correct?

This message was sent by Atlassian JIRA

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

Reply via email to