我有log4jConfig.xml,如下所示
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="false" xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="abcLog4j" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/myapp/app/myserver/myproj/domains/logs/abcLog.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="5000KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>
</layout>
</appender>
<logger name="com.mywhole.mysub.abc" additivity="false">
<level value="INFO"/>
<appender-ref ref="abcLog4j"/>
</logger>
<appender name="xyzLog4j" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/myapp/app/myserver/myproj/domains/logs/xyzLog.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="5000KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>
</layout>
</appender>
<logger name="com.mywhole.mysub.xyz" additivity="false">
<level value="INFO"/>
<appender-ref ref="xyzLog4j"/>
</logger>
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>
</layout>
</appender>
.......................
<root>
<priority value="ERROR"/>
<appender-ref ref="sysoutLog4j"/>
</root>
</log4j:configuration>
我面临的问题是,从com.mywhole.mysub.xyz中的类生成的日志语句将进入abcLog.log,反之亦然。有人能告诉我如何解决这个问题吗
package com.mywhole.mysub.xyz.model;
// import ....
public class MyBeackingBean extends MySuperBB{
public static final LoggerInterface log = LoggerFactory
.getLogger(MyBeackingBean.class);
log4j的xml format
应遵循以下模式
<!ELEMENT log4j:configuration (renderer*, appender*,(category|logger)*,root?, categoryFactory?)>
所有的附加程序都应该是declared before
记录器
所以你的log4j.xml
应该看起来像下面的
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="false" xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="abcLog4j" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/myapp/app/myserver/myproj/domains/logs/abcLog.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="5000KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>
</layout>
</appender>
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>
</layout>
</appender>
<appender name="xyzLog4j" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/myapp/app/myserver/myproj/domains/logs/xyzLog.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="5000KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>
</layout>
</appender>
<logger name="com.mywhole.mysub.xyz" additivity="false">
<level value="INFO"/>
<appender-ref ref="xyzLog4j"/>
</logger>
<logger name="com.mywhole.mysub.abc" additivity="false">
<level value="INFO"/>
<appender-ref ref="abcLog4j"/>
</logger>
.......................
<root>
<priority value="ERROR"/>
<appender-ref ref="sysoutLog4j"/>
</root>
查看此链接以获取更多详细信息。
配置看起来是正确的,它看起来像是代码中的复制粘贴错误。假设LoggerFactory
是org.slf4j.LoggerFactory
,LoggerInterface
是org.slf4j.Logger
,请检查中的MyBeanClass.class
package com.mywhole.mysub.xyz;
// ...
LoggerFactory.getLogger(MyBeanClass.class);
不是CCD_ 9。