本文介绍了log4j将downsteam的日志写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Java应用程序.我已经在src文件夹下创建了一个log4j2.xml,并且可以按预期工作.
I have a java application. I have created a log4j2.xml under src folder and that works as expected.
此Java应用程序依赖于另一个Java程序包.我尝试在此程序包中创建一个类似的log4j2.xml来格式化在程序包中完成的所有日志记录,但这似乎不起作用.软件包中的所有日志似乎都将以默认格式进行控制台.
This java application depends on another java package. I tried creating a similar log4j2.xml in this package to format all the logging done in package, But this does not seem to work. And all the logs in the package seems to be going to console in default formatting.
有想法吗?
推荐答案
在这种情况下,您需要具有不同的Appenders
In this case you need to have different Appenders
因此配置将如下所示
<!-- Package 1 appender declaration -->
<appender name="one" class="org.apache.log4j.FileAppender">
...
</appender>
<!-- Package 2 appender declaration -->
<appender name="two" class="org.apache.log4j.FileAppender">
...
</appender>
<!-- logger package com.myapp.app.one -->
<logger name="com.myapp.app.one">
<level value="DEBUG" />
<appender-ref ref="one"/>
</logger>
<!-- logger for package com.myapp.app.two -->
<logger name="com.myapp.app.two">
<level value="DEBUG" />
<appender-ref ref="two"/>
</logger>
这篇关于log4j将downsteam的日志写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!