当前位置:编程学习 > JAVA >>

log4j分级别记录日志,借鉴也可以分模块记日志

Java代码 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> 
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'> 
     
    <appender name="stdout" class="org.apache.log4j.ConsoleAppender"> 
        <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" 
                value="%d{yyyy/MM/dd HH:mm:ss.sss} [%-5p] %c - %m%n"/> 
        </layout> 
    </appender> 
     
    <appender name="traceLog4Debug" class="org.apache.log4j.RollingFileAppender"> 
        <param name="File" value="C:/traceLog4Debug.log"/> 
        <param name="maxFileSize" value="2000KB"/> 
        <param name="maxBackupIndex" value="20"/> 
        <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" 
                value="%d{yyyy/MM/dd HH:mm:ss.sss} [%-5p] %c - %m%n"/> 
        </layout> 
    </appender> 
     
    <appender name="debugLog" class="org.apache.log4j.RollingFileAppender"> 
        <param name="File" value="C:/debug.log"/> 
        <param name="maxFileSize" value="2000KB"/> 
        <param name="maxBackupIndex" value="10"/> 
        <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" 
                value="%d{yyyy/MM/dd HH:mm:ss.sss} [%-5p] %c - %m%n"/> 
        </layout> 
        <SPAN style="COLOR: #ff0000"><!-- filter作用?  配置LevelMax与LevelMin限定输出到文件的日志级别 --> 
        <filter class="org.apache.log4j.varia.LevelRangeFilter"> 
                        <param name="LevelMax" value="DEBUG" /> 
                        <param name="LevelMin" value="DEBUG" /> 
                </filter></SPAN> 
    </appender> 
     
    <appender name="infoLog" class="org.apache.log4j.RollingFileAppender"> 
        <param name="File" value="C:/info.log"/> 
        <param name="maxFileSize" value="2000KB"/> 
        <param name="maxBackupIndex" value="10"/> 
        <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" 
                value="%d{yyyy/MM/dd HH:mm:ss.sss} [%-5p] %c - %m%n"/> 
        </layout> 
        <filter class="org.apache.log4j.varia.LevelRangeFilter"> 
                    <param name="LevelMax" value="INFO" /> 
                    <param name="LevelMin" value="INFO" /> 
            </filter> 
    </appender> 
     
    <appender name="warnLog" class="org.apache.log4j.RollingFileAppender"> 
        <param name="File" value="C:/warn.log"/> 
        <param name="maxFileSize" value="2000KB"/> 
        <param name="maxBackupIndex" value="10"/> 
        <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" 
                value="%d{yyyy/MM/dd HH:mm:ss.sss} [%-5p] %c - %m%n"/> 
        </layout> 
        <filter class="org.apache.log4j.varia.LevelRangeFilter"> 
                    <param name="LevelMax" value="WARN" /> 
                    <param name="LevelMin" value="WARN" /> 
            </filter> 
    </appender> 
     
    <appender name="errorLog" class="org.apache.log4j.RollingFileAppender"> 
        <param name="File" value="C:/error.log"/> 
        <param name="maxFileSize" value="2000KB"/> 
        <param name="maxBackupIndex" value="10"/> 
       

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,