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

Location-based functionality broken in AbstractLoggerWrapper subclasses

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.0-rc1
    • 2.0-rc2
    • API, Core
    • None

    Description

      How to reproduce

      • Create a custom logger that extends AbstractLoggerWrapper (or generate one with the tool attached to LOG4J2-519)
      • In the custom logger provide a public method that invokes the log(Level, String) method
      • Configure a pattern layout that uses location, like %C for the logger FQCN
      • From a sample app, call the public method on your custom logger.
      • The output will show the class name of the custom logger instead of the class name of the calling class in the sample application.

      Cause
      AbstractLogger's FQCN field is static final and initialized to AbstractLogger.class.getName(). Then, in Log4jLogEvent#calcLocation(), when walking over the stack trace elements, the element following the FQCN is returned. So only loggers that directly subclass from AbstractLogger will work correctly. Loggers that inherit from AbstractLoggerWrapper are two levels removed from AbstractLogger and the calcLocation() method will not work correctly.

      Solution
      I think AbstractLogger's FQCN field should be made non-static, and initialized to getClass().getName() in the constructor of AbstractLogger. Log4jLogEvent#calcLocation() can then be modified to return the StackElement whose class name matches the FQCN, instead of the next element. Location-based functionality should then work for arbitrarily deep subclass hierarchies of AbstractLogger.

      Attachments

        1. log4j2-555-gg-v3.diff
          127 kB
          Gary D. Gregory
        2. LOG4J2-555-delegate.patch
          43 kB
          Remko Popma
        3. log4j2-555-bbrouwer-2.patch
          130 kB
          Bruce Brouwer
        4. log4j2-555-bbrouwer.patch
          125 kB
          Bruce Brouwer

        Issue Links

          Activity

            People

              rpopma Remko Popma
              rpopma Remko Popma
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: