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

Cannot extend LogPatternConverter

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: 2.9.1, 2.11.1
    • Fix Version/s: None
    • Component/s: Core
    • Labels:

      Description

      It seems that when I want to extend LogPatternConverter, you quickly get an IllegalAccessException. The source of that problem seems to be the method PatternParser.finalizeConverter().

      On one hand I should be able to create the static newInstance method in my newly created class (variable "newInstanceMethod"). Otherwise the method is not assigned:

      final Method[] methods = converterClass.getDeclaredMethods();         
      Method newInstanceMethod = null;         
      for (final Method method : methods) {             
        if (Modifier.isStatic(method.getModifiers()) && method.getDeclaringClass().equals(converterClass)
                           && method.getName().equals("newInstance")) {
                       if (newInstanceMethod == null) {
                           newInstanceMethod = method;
                       } else if (method.getReturnType().equals(newInstanceMethod.getReturnType())) {
                           LOGGER.error("Class " + converterClass + " cannot contain multiple static newInstance methods");
                           return null;
                       }
                   }
               }

      On the other hand, "newInstanceMethod.invoke" is called without calling newInstanceMethod.setAccessible(true)

                  final Object newObj = newInstanceMethod.invoke(null, parms);
      

      But your extended class would normally be ourside the package of PatternParser. Triggering an IllegalAccessException.

      A possible workaround is to put your new class in the package org.apache.logging.log4j.core.pattern yourself. But that is not how we normally extend classes .

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              DavidSoft.nl David H
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated: