日志语句不会进入提到的日志文件

时间:2013-02-06 07:57:51

标签: jsf java-ee logging log4j

我有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);

2 个答案:

答案 0 :(得分:0)

配置看起来正确,它看起来像代码中的复制粘贴错误。假设LoggerFactoryorg.slf4j.LoggerFactoryLoggerInterfaceorg.slf4j.Logger,请检查MyBeanClass.class是否在<{p>}

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

不是com.mywhole.mysub.abc.MyBeanClass

答案 1 :(得分:0)

log4j的xml format应该遵循这种模式

<!ELEMENT log4j:configuration (renderer*, appender*,(category|logger)*,root?, categoryFactory?)>   

所有appender都应该是declared before loggers

所以你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>

点击此链接了解详情here