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

Using method references to Logger methods leads to wrong source file and line number in log entry

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 2.9.1, 2.10.0, 2.11.1
    • Fix Version/s: None
    • Component/s: None

      Description

      The following example produces a log message with origin Optional.ifPresent which is wrong.

      package com.exxeta.log4j.test;
      
      import java.util.Optional;
      import java.util.function.Consumer;
      
      import org.apache.logging.log4j.Level;
      import org.apache.logging.log4j.LogManager;
      import org.apache.logging.log4j.Logger;
      import org.apache.logging.log4j.core.appender.ConsoleAppender;
      import org.apache.logging.log4j.core.config.Configurator;
      import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder;
      import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
      import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
      import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
      
      public class Log4jThreadAnalyzerErrorDemo
      {
          private static final Logger           LOG;
      
          static
          {
              configureRootLogger();
              LOG = LogManager.getRootLogger();
          }
      
          public static void main(String[] args)
          {
              // This produces "INFO main - (Optional.java:159) - Optional.ifPresent: Hello World!"
              Optional.of("Hello World!").ifPresent(LOG::info);
          }
      
          private static void configureRootLogger()
          {
              ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory
                      .newConfigurationBuilder();
              AppenderComponentBuilder console = builder.newAppender("Stdout", "CONSOLE").addAttribute("target",
                      ConsoleAppender.Target.SYSTEM_OUT);
              console.add(builder.newLayout("PatternLayout").addAttribute("pattern",
                      "%n%d{dd.MM.yyyy HH:mm:ss} %-5p %t - (%F:%L) - %C{1}.%M: %m%n"));
              builder.add(console);
              builder.add(builder.newRootLogger(Level.INFO).add(builder.newAppenderRef("Stdout")));
              Configurator.initialize(builder.build());
          }
      }
      

      This may or may not be related to LOG4J2-1570.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                stefan.kuettner Stefan K├╝ttner
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: