作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用StringWriter
作为WriterAppender
的Target
。有一次,我将我的日志写入 StringWriter
,我想将这些日志发送为我的方法的响应。
我当前的 log4j2.xml -
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="TRACE">
<Appenders>
<Writer name="MyAppender" target="StringWriter">
<JsonLayout/>
</Writer>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="MyAppender"/>
</Root>
</Loggers>
</Configuration>
这给了我以下错误 -
2017-08-19 20:07:49,359 main ERROR Writer contains invalid attributes "name", "target"
2017-08-19 20:07:49,359 main ERROR appender Writer has no parameter that matches element JsonLayout
2017-08-19 20:07:49,359 main ERROR Unable to inject fields into builder class for plugin type class org.apache.logging.log4j.core.appender.WriterAppender, element Writer. java.lang.NullPointerException
at org.apache.logging.log4j.core.appender.WriterAppender.getManager(WriterAppender.java:168)
at org.apache.logging.log4j.core.appender.WriterAppender.access$000(WriterAppender.java:35)
at org.apache.logging.log4j.core.appender.WriterAppender$Builder.build(WriterAppender.java:56)
我正在寻找正确的 XML 配置(首选)如果 WriterAppender
无法使用 XML,则采用编程配置。
最佳答案
我认为 log4j2
中存在的 WriterAppender 不支持 XML 配置。
我可以使用下面的示例代码以编程方式配置它 -
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();
int i = 1;
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();
}
}
您需要在 classpath
上有 log4j2
配置文件,以避免出现以下错误消息 -
ERROR StatusLogger No log4j2 configuration file found. Using default
configuration: logging only errors to the console. Set system property
'org.apache.logging.log4j.simplelog.StatusLogger.level' to TRACE to
show Log4j2 internal initialization logging.
关于java - 如何在log4j2 xml中配置WriterAppender?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45771959/
本文整理了Java中org.apache.logging.log4j.core.appender.WriterAppender.newBuilder()方法的一些代码示例,展示了WriterAppen
本文整理了Java中org.apache.logging.log4j.core.appender.WriterAppender.createAppender()方法的一些代码示例,展示了WriterA
我是一名优秀的程序员,十分优秀!