You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Ralph Goers (Jira)" <ji...@apache.org> on 2021/12/05 22:19:00 UTC

[jira] [Commented] (LOG4J2-2529) Cannot extend LogPatternConverter

    [ https://issues.apache.org/jira/browse/LOG4J2-2529?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17453702#comment-17453702 ] 

Ralph Goers commented on LOG4J2-2529:
-------------------------------------

I must be missing something. The newInstance method is a static member of your Converter. It should be declared public. The constructor in LogEventPatternConverter is protected as it can only be accessed by classes that extend it.  I do not understand why you would have to put this into the org.apache package as PatternParser should have no problem calling a public newInstance method.

Also, I wouldn't have implemented masking this way. I would have done it as %mask{%m}{ssn,${ONLY_LAST_FOUR},creditCard,${ONLY_LAST_FOUR}} where ONLY_LAST_FOUR is a property that contains a regex to mask everything but the last 4 digits. This would not need to extend the LogEventPatternConverter as the resolved message would be passed to the MaskConverter.

> Cannot extend LogPatternConverter
> ---------------------------------
>
>                 Key: LOG4J2-2529
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-2529
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Core
>    Affects Versions: 2.9.1, 2.11.1
>            Reporter: David H
>            Priority: Minor
>              Labels: log4j2-newbie
>
> 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:
> {code:java}
> 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;
>                  }
>              }
>          }{code}
> On the other hand, "newInstanceMethod.invoke" is called without calling newInstanceMethod.setAccessible(true)
> {code:java}
>             final Object newObj = newInstanceMethod.invoke(null, parms);
> {code}
> 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 ;).



--
This message was sent by Atlassian Jira
(v8.20.1#820001)