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

TestAppender based on MutableLogEvent stop working in Log4j2.11



    • Question
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • 2.11.2
    • 2.8
    • None
    • None
    • JDK 11, Log4J-2.11.2


      I have written unit tests on Logs by creating a custom appender

       After moving to Log4j2.11, I am seeing that the getMessages is returning empty after I am logging something. I attached a debugger and can see that the append() method is being invoked and the message list is getting populated, however at the time of verififcation it is magically vanishing off. Reverting back to 2.8 fixes this.

      import java.io.Serializable;
      import java.util.ArrayList;
      import java.util.List;
      import org.apache.logging.log4j.core.Filter;
      import org.apache.logging.log4j.core.Layout;
      import org.apache.logging.log4j.core.LogEvent;
      import org.apache.logging.log4j.core.appender.AbstractAppender;
      import org.apache.logging.log4j.core.config.plugins.Plugin;
      import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
      import org.apache.logging.log4j.core.config.plugins.PluginElement;
      import org.apache.logging.log4j.core.config.plugins.PluginFactory;
      import org.apache.logging.log4j.core.impl.MutableLogEvent;
      import org.apache.logging.log4j.core.layout.PatternLayout;
      @Plugin(name = "TestAppender", category = "Core", elementType = "appender", printObject = true)
      public class TestAppender extends AbstractAppender {
          private List<String> messages = new ArrayList<>();
          TestAppender(String name, Filter filter, Layout<? extends Serializable> layout) {
              super(name, filter, layout);
          @Override public void append(LogEvent event) { 
              getMessages().add(((MutableLogEvent) (event)).getFormattedMessage()); 
          @PluginFactory public static TestAppender createAppender(
                  @PluginAttribute("name") String name,
                  @PluginElement("Layout") Layout<? extends Serializable> layout,
                  @PluginElement("Filter") final Filter filter,
                  @PluginAttribute("otherAttribute") String otherAttribute) {
              if (name == null) {
                  LOGGER.error("No name provided for TestAppender");
                  return null;
              if (layout == null) { 
                  layout = PatternLayout.createDefaultLayout(); 
              return new TestAppender(name, filter, layout);
          public List<String> getMessages() {
              return messages;
          public void clearAppender() {
              this.messages = new ArrayList<>();

      Test log configuration file

      <?xml version="1.0" encoding="UTF-8"?>
      <Configuration status="WARN" packages="com.sample.package.test.utils">
          <Property name="log-path">${sys:root}/var/output/logs</Property>
          <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
          <TestAppender name="TestAppender">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
          <Logger name="com.amazon.sample.package" level="ALL"/>
            <AppenderRef ref="TestAppender" level="ALL"/>




            Unassigned Unassigned
            adwsingh Adwait Kumar Singh
            0 Vote for this issue
            2 Start watching this issue