在Log4j
中有一个WriterAppender
,可以将日志写入Writer
。
我需要在Log4j2
中使用相同的功能,但我还没有找到执行此操作的选项。有谁知道如何使用Log4j2
实现相同的目标?
答案 0 :(得分:1)
如果没有满足您需求的现有appender,请创建一个自定义appender插件。
我把一个简单的例子放在一起。
package com.logging;
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.layout.PatternLayout;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.Writer;
@Plugin(name = "WriterAppender", category = "Core", elementType = "appender", printObject = false)
public class WriterAppender extends AbstractAppender{
private Writer writer;
protected WriterAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions, String argOne, String argTwo) {
super(name, filter, layout, ignoreExceptions);
writer = new PrintWriter(System.out);//init or retrieve writer resource upon construction
}
@Override
public void append(LogEvent logEvent) {
try {
writer.write(logEvent.getMessage().getFormattedMessage());
} catch (IOException e) {
e.printStackTrace();
}
}
@PluginFactory
public static WriterAppender createAppender(
@PluginAttribute("name") String name,
@PluginElement("Layout") Layout<? extends Serializable> layout,
@PluginElement("Filter") final Filter filter,
@PluginAttribute("otherAttribute") String otherAttributeOne,
@PluginAttribute("otherAttributeTwo") String otherAttributeTwo
) {
if (name == null) {
LOGGER.error("No name provided for MyCustomAppenderImpl");
return null;
}
if (layout == null) {
layout = PatternLayout.createDefaultLayout();
}
return new WriterAppender(name, filter, layout, true, otherAttributeOne,otherAttributeTwo);
}
}
您可以像使用任何其他appender一样使用配置添加WriterAppender。
答案 1 :(得分:0)
虽然这是一个老问题,但我想添加答案以供将来参考。
WriterAppender
中存在 log4j2
。以下是使用程序化配置进行配置的一种方法 -
package example;
import java.io.StringWriter;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.WriterAppender;
import org.apache.logging.log4j.core.config.AppenderRef;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.layout.PatternLayout;
public class App {
private static final Logger LOGGER = LogManager.getLogger(App.class);
private static StringWriter stringWriter = new StringWriter();
public static void main(String[] args) {
createLogger();
LOGGER.info("It is info log - {}", i);
LOGGER.warn("It is warn log - {} ", i);
LOGGER.error("It is error log");
System.out.println(stringWriter.toString());
}
private static void createLogger() {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
PatternLayout layout = PatternLayout.newBuilder()
.withPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] [%c] [%M] [%l] - %msg%n").build();
WriterAppender writerAppender = WriterAppender.newBuilder().setName("writeLogger").setTarget(stringWriter)
.setLayout(layout).build();
writerAppender.start();
config.addAppender(writerAppender);
AppenderRef ref = AppenderRef.createAppenderRef("writeLogger", null, null);
AppenderRef[] refs = new AppenderRef[] { ref };
LoggerConfig loggerConfig = LoggerConfig.createLogger(false, Level.INFO, "example", null, refs, null, config,
null);
loggerConfig.addAppender(writerAppender, null, null);
config.addLogger("example", loggerConfig);
ctx.updateLoggers();
}
}