0

我有如下所示的 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);
4

2 回答 2

0

配置看起来正确,它看起来像代码中的复制粘贴错误。假设LoggerFactoryisorg.slf4j.LoggerFactoryLoggerInterfaceis org.slf4j.Logger,请检查MyBeanClass.classin

package com.mywhole.mysub.xyz;
// ...
LoggerFactory.getLogger(MyBeanClass.class);

不是com.mywhole.mysub.abc.MyBeanClass

于 2013-02-06T11:23:12.860 回答
0

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>

在此处查看此链接以获取更多详细信息

于 2013-02-06T11:56:05.150 回答