问题描述
我需要实现多个记录器和多个追加器.我的log4j2.xml如下所示:
I need to implement multiple loggers and multiple appenders. My log4j2.xml looks like below:
<Appenders>
<RollingFile name="SYSTEM_LOGGER"
fileName="${logging.folder}System.log"
filePattern="${ARCHIVE}System.log.%d{dd MMM yyyy HH:mm:ss.SSS}.gz">
<PatternLayout>
<Pattern>${PATTERN}</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="10" modulate="true"/>
<SizeBasedTriggeringPolicy size="4 MB" />
<DefaultRolloverStrategy max="50"/>
</Policies>
</RollingFile>
<Appenders>
<RollingFile name="COMMONREQ_LOGGER"
fileName="${logging.folder}/CommonReq.log"
filePattern="${ARCHIVE}/CommonReq.log.%d{dd MMM yyyy HH:mm:ss.SSS}.gz">
<PatternLayout>
<Pattern>${PATTERN}</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="10" modulate="true"/>
<SizeBasedTriggeringPolicy size="4 MB" />
<DefaultRolloverStrategy max="50"/>
</Policies>
</RollingFile>
<Appenders>
<RollingFile name="COMMONRES_LOGGER"
fileName="${logging.folder}/CommonRes.log"
filePattern="${ARCHIVE}/CommonRes.log.%d{dd MMM yyyy HH:mm:ss.SSS}.gz">
<PatternLayout>
<Pattern>${PATTERN}</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="10" modulate="true"/>
<SizeBasedTriggeringPolicy size="4 MB" />
<DefaultRolloverStrategy max="50"/>
</Policies>
</RollingFile>
<Loggers>
<Root level="INFO">
<AppenderRef ref="SYSTEM_LOGGER"/>
<AppenderRef ref="COMMONREQ_LOGGER"/>
<AppenderRef ref="COMMONRES_LOGGER"/>
</Root>
</Loggers>
现在,当我使用此xml执行代码时,日志将写入最后一个日志文件CommonRes.log.我是log4j的新手.如何只写到所需的日志文件?
Now, when I execute the code using this xml, the log is written to the last log file CommonRes.log. I'm new to log4j. How can I write only to the desired log file?
这是我到目前为止在log4j2.xml中所做的:
This is what I have done so far in log4j2.xml:
<Routing name="Routing">
<Routes pattern="$${ctx:ROUTINGKEY}">
<Route key="$${ctx:ROUTINGKEY}" >
<RollingFile name="SYSTEM_LOGGER"
fileName="${logging.folder}/$${ctx:ROUTINGKEY}.log"
filePattern="${ARCHIVE}/$${ctx:ROUTINGKEY}.log.%d{dd MMM yyyy HH:mm:ss.SSS}.gz">
<PatternLayout>
<Pattern>${PATTERN}</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="10" modulate="true"/>
<SizeBasedTriggeringPolicy size="4 MB"/>
<DefaultRolloverStrategy max="50"/>
</Policies>
</RollingFile>
</Route>
</Routes>
</Routing>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Routing" />
</Root>
在我的Java代码中:
And in my java code:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger = LogManager.getLogger(request.getPathInfo().replace("/", ""));
...
if(logger.getLevel() != null){
ThreadContext.put("ROUTINGKEY", request.getPathInfo().replace("/", ""));
logger.info(contents);
}
}
当我运行上面的代码时,它不会写入任何文件,而是出现以下错误:
When I run the above code, it doesn't write to any file, instead gives the following error:
ERROR Unknown object "Routing" of type org.apache.logging.log4j.core.appender.routing.RoutingAppender is ignored.
ERROR Unable to locate appender Routing for logger
请帮助.
推荐答案
您可以在配置中定义多个路由,并将值放在ThreadContext映射中,以确定此线程中后续事件记录到的日志文件.您可以从此链接开始.
You can define multiple routes in the configuration, and put values in the ThreadContext map that determine which log file subsequent events in this thread get logged to. THIS LINK is where you can start from.
基本概念取决于线程上下文映射中的值,您可以将日志路由到其他文件.例如:
The basic concept is depending upon values in your threadcontext map, you can route your logs to different files. For example:
<Routing name="Routing">
<Routes pattern="$${ctx:variable}">
<!-- This route is chosen if thread context has no value for key 'variable' -->
<Route key="$${ctx:variable}">
<RollingFile name="Rolling-default" fileName="default.log"
filePattern="./logs/${date:yyyy-MM}/default-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout>
<pattern>%d{ISO8601} [%t] %p %c{3} - %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true" />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
</RollingFile>
</Route>
<!-- This route is chosen if thread context has some value for key 'variable' -->
<Route>
<File name="variable-${ctx:variable}" fileName="other.log">
</File>
</Route>
</Routes>
</Routing>
fileName ="default.log" 和 fileName ="other.log" 是您各自路由的文件路径仅添加一个追加程序.根据线程上下文变量,它将写入不同的文件
fileName="default.log" and fileName="other.log" is your file path for your respective routes Add one appender only. Depending upon thread context variable it will write to different file
<Loggers>
<Root level="info">
<AppenderRef ref="Routing" />
</Root>
</Loggers>
这篇关于使用多个附加程序和记录器的Log4j2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!