如何正确配置log4j

时间:2014-07-15 14:23:25

标签: java logging log4j

log4j配置有问题。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

    <appender name="scApp" class="org.apache.log4j.FileAppender">
        <param name="File" value="connector.txt"/>
        <param name="immediateFlush" value="true"/>
        <param name="Threshold" value="INFO"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n"/>
        </layout>
    </appender>


    <logger name="com.mypackage.webservice">
        <level value="INFO" />
        <appender-ref ref="scApp" />        
    </logger >

</log4j:configuration>

我将该文件放到/ WEB-INF / classes /但没有任何反应。文件未创建,但它应该导致我使用方法logger.info();

有没有办法强制api使用它?

1 个答案:

答案 0 :(得分:0)

可能是您想要的:

<!-- root -->
<root>
  <priority value="TRACE" />
  <appender-ref ref="ROLLOUT" />
</root>

完整示例:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" 
                                     "log4j.dtd" >
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

  <!-- file -->
  <appender name="ROLLOUT" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="/tmp/output.log" />
    <param name="maxFileSize" value="1MB" />
    <param name="maxBackupIndex" value="5" />
    <param name="threshold" value="DEBUG" />
    <param name="encoding" value="UTF-8" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="conversionPattern" 
             value="%d{yyyyMMdd-HHmmss.SSS} %-5p (%c.java:%L).%M - %m%n" />
    </layout>
  </appender>

  <!-- categories -->
  <category name="org.apache.commons">
    <priority value="OFF" />
  </category>
  <category name="org.hibernate">
    <priority value="OFF" />
  </category>

  <!-- root -->
  <root>
    <priority value="TRACE" />
    <appender-ref ref="ROLLOUT" />
  </root>

</log4j:configuration>