log4j2-test.properties文件模式布局

ubby3x7f  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(337)

我正在编写一个单元测试,它使用slf4j作为api,使用log4j2作为日志实现。我有一个习惯 AbstractAppender (不使用 LogEvent 到目前为止)但我正在尝试输出 LogEvent 消息和格式化消息首先使用 System.out 为了确保我得到我所期望的——在这种情况下我没有。
我期待着见到你 "---WCustomAppender---" 作为根据我的属性文件模式输出消息的前缀,但该模式似乎没有被提取到我的appender中。
有什么东西是我缺少的,比如一处房产等吗?
所有代码如下:
test/resources/log4j2-test.properties

packages = learning.logging
appenders=WCustomAppender

rootLogger.level=ERROR
rootLogger.appenderRefs=WCustomAppender
rootLogger.appenderRef.stdout.ref=WCustomAppender

appender.WCustomAppender.name=WCustomAppender
appender.WCustomAppender.type=WCustomAppender
appender.WCustomAppender.layout.type=PatternLayout
appender.WCustomAppender.layout.pattern=---WCustomAppender--- %m %n

测试/学习/记录/wcustomappender

package learning.logging;

import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Core;
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.Property;
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 java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Plugin(
        name = "WCustomAppender",
        category = Core.CATEGORY_NAME,
        elementType = Appender.ELEMENT_TYPE)
class WCustomAppender extends AbstractAppender {

  private static final List<LogEvent> LOG_EVENTS = new ArrayList<>();

  protected WCustomAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions, Property[] properties) {
    System.out.println("layout loaded in appender from constructor: " + layout);
    super(name, filter, layout, ignoreExceptions, properties);
  }

  @PluginFactory
  public static WCustomAppender createAppender(
          @PluginAttribute("name") String name,
          @PluginElement("Filter") Filter filter,
          @PluginElement("Layout") Layout<? extends Serializable> layout) {
    return new WCustomAppender(name, filter, layout, false, Property.EMPTY_ARRAY);
  }

  public List<LogEvent> getLogEvents() {
    return LOG_EVENTS;
  }

  @Override
  public void append(LogEvent event) {
    System.out.println("message: " + event.getMessage());
    System.out.println("message (formatted): " + event.getMessage().getFormattedMessage());
    LOG_EVENTS.add(event);
  }
}

测试/学习/记录/日志测试仪

package learning.logging;

import lab.error.logging.ErrorLogger;
import lab.error.logging.ErrorStatus;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogTester {

  private Logger log = LoggerFactory.getILoggerFactory().getLogger("LogTester");

  @Test
  public void appenderPOC() {
    log.error("My custom appender POC");
  }
}

控制台输出:

layout loaded in appender from constructor: ---WCustomAppender--- %m %n
message: My custom appender POC
message (formatted): My custom appender POC
5gfr0r5j

5gfr0r5j1#

我可能发现了这个问题:
我发现 AbstractAppender 有一个名为 toSerializable 检查是否存在布局(在我的例子中是这样),然后将其应用于 LogEvent . 所以在我的 WCustomAppender 我关掉了电视 getLogEvents 返回 Serializable 而是在测试中进行验证。

/**
     * Serializes the given event using the appender's layout if present.
     *
     * @param event
     *            the event to serialize.
     * @return the serialized event or null if no layout is present.
     */
    protected Serializable toSerializable(final LogEvent event) {
        return layout != null ? layout.toSerializable(event) : null;
    }

控制台输出:

message: ---WCustomAppender--- My custom appender POC

相关问题