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

log4j-slf4j-impl not use MessageFactory2

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.14.1
    • 3.0.0
    • SLF4J Bridge
    • JDK 1.8.0.192

      slf4j-api 1.7.30 ,

      log4j-slf4j-impl 2.14.1,

      log4j-api 2.14.1

      log4j-core 2.14.1.

    Description

      Hi,

      I'm using slf4j-api 1.7.30 ,log4j-slf4j-impl 2.14.1,log4j-api 2.14.1 and log4j-core 2.14.1.

      Then I config log4j2.component.properties with:

      log4j2.messageFactory=com.lizongbo.log4j2.DataMaskingMessageFactory

       

      code:

      public class DataMaskingMessageFactory extends AbstractMessageFactory {
      private static final long serialVersionUID = 1L;

      @Override
      public Message newMessage(Object message) {
      return new DataMaskingParameterizedMessage("{}", message);
      }

      @Override
      public Message newMessage(String message)

      { return new DataMaskingParameterizedMessage(message); }

      @Override
      public Message newMessage(String message, Object... params)

      { return new DataMaskingParameterizedMessage(message, params); }

      }

      So I can do datamask for every log content.

       

       

      ButI found Mybatis log content not datamasked.

      org.apache.logging.slf4j.Log4jLogger  log method code:

       

      @Override
      public void log(final Marker marker, final String fqcn, final int level, final String message, final Object[] params, Throwable throwable) {
      final Level log4jLevel = getLevel(level);
      final org.apache.logging.log4j.Marker log4jMarker = getMarker(marker);

      if (!logger.isEnabled(log4jLevel, log4jMarker, message, params))

      { return; }
      final Message msg;
      if (CONVERTER != null && eventLogger && marker != null && marker.contains(EVENT_MARKER)) { msg = CONVERTER.convertEvent(message, params, throwable); } else if (params == null) { msg = new SimpleMessage(message); // bug: not use messageFactory.newMessage } else {
      msg = new ParameterizedMessage(message, params, throwable); // bug: not use messageFactory.newMessage
      if (throwable != null) { throwable = msg.getThrowable(); }
      }
      logger.logMessage(fqcn, log4jLevel, log4jMarker, msg, throwable);
      }

       

      and org.apache.logging.log4j.spi.AbstractLogger code:

      protected void logMessage(final String fqcn, final Level level, final Marker marker, final String message,
      final Object... params) { final Message msg = messageFactory.newMessage(message, params);//use messageFactory.newMessage logMessageSafely(fqcn, level, marker, msg, msg.getThrowable()); }

       

      So  org.apache.logging.slf4j.Log4jLogger log method also need   use messageFactory.newMessage to create  Message object.

       

      just like this:

       

      -----------------------------------
      private final MessageFactory2 messageFactory;

      public Log4jLogger(final ExtendedLogger logger, final String name) { this.logger = logger; this.eventLogger = "EventLogger".equals(name); this.name = name; this.messageFactory = createDefaultMessageFactory(); }
      private static MessageFactory2 createDefaultMessageFactory() {
      try { final MessageFactory result = org.apache.logging.log4j.spi.AbstractLogger.DEFAULT_MESSAGE_FACTORY_CLASS.newInstance(); return narrow(result); } catch (final InstantiationException | IllegalAccessException e) { throw new IllegalStateException(e); }
      }

      @Override
      public void log(final Marker marker, final String fqcn, final int level, final String message, final Object[] params, Throwable throwable) {
      final Level log4jLevel = getLevel(level);
      final org.apache.logging.log4j.Marker log4jMarker = getMarker(marker);

      if (!logger.isEnabled(log4jLevel, log4jMarker, message, params)) { return; }

      final Message msg;
      if (CONVERTER != null && eventLogger && marker != null && marker.contains(EVENT_MARKER))

      { msg = CONVERTER.convertEvent(message, params, throwable); }

      else if (params == null)

      { //msg = new SimpleMessage(message); msg = messageFactory.newMessage(message, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9); }

      else {
      //msg = new ParameterizedMessage(message, params, throwable);
      msg = messageFactory.newMessage(message, params);
      if (throwable != null)

      { throwable = msg.getThrowable(); }

      }
      logger.logMessage(fqcn, log4jLevel, log4jMarker, msg, throwable);
      }

      -----------------------------------

       

       

       

       

       

      log4j2.component.properties

      Attachments

        Issue Links

          Activity

            People

              rgoers Ralph Goers
              lizongbo lizongbo
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: