Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-1585

Logger.getParent gives warning about MessageFactory mismatch

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.6.2
    • None
    • None
    • None

    Description

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

              final MessageFactory messageFactory = getMessageFactory();
              if (context.hasLogger(lcName, messageFactory)) {          // <=====
                  return context.getLogger(lcName, messageFactory);   // <=====
              }
              return new Logger(context, lcName, messageFactory);
      

      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:

              LogManager.getRootLogger();
              Logger logger = LogManager.getLogger("foo");
              ((org.apache.logging.log4j.core.Logger) logger).getParent();
      

      I was thinking about a fix like this:

              if (context.hasLogger(lcName)) {
                  return context.getLogger(lcName);
              }
              return new Logger(context, lcName, getMessageFactory());
      

      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?

      Attachments

        Activity

          People

            Unassigned Unassigned
            shangdi Di Shang
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated: