log4j_learn...

log4j.propoties

 perfect blog : Here...

 

 

log4j.xml

   config info -> Here ...

   config log file path -> Here ...

 

===========================================================

base technology:

ConversionPattern参数的格式含义 :

格式名    含义 
%c 输出日志信息所属的类的全名 
%d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy-MM-dd HH:mm:ss },输出类似:2002-10-18- 22:10:28 
%f 输出日志信息所属的类的类名 
%l 输出日志事件的发生位置,即输出日志信息的语句处于它所在的类的第几行 
%m 输出代码中指定的信息,如log(message)中的message 
%n 输出一个回车换行符,Windows平台为“rn”,Unix平台为“n” 
%p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL。如果是调用debug()输出的,则为DEBUG,依此类推 
%r 输出自应用启动到输出该日志信息所耗费的毫秒数 
%t 输出产生该日志事件的线程名

c: full name class   f: only class name

d: date                  l: line

m:message           n: enter line

p: priority              r:    毫秒                        t:thread name

 

Log4j建议只使用四个级别,优先级从高到低分别是ERROR、WARN、INFO、DEBUG 
#这里定义能显示到的最低级别,若定义到INFO级别,则看不到DEBUG级别的信息了~! 

 

log4j.xml

 

<?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/'>

    <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:%L - %m%n"/>
        </layout>
    </appender>

    <appender name="traceLog4Debug" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="c:/log/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:%L - %m%n"/>
        </layout>
    </appender>

    <appender name="debugLog" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="c:/log/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:%L - %m%n"/>
        </layout>
       <filter class="org.apache.log4j.varia.LevelRangeFilter">
                <param name="LevelMax" value="DEBUG" />
                <param name="LevelMin" value="DEBUG" />
       </filter>
    </appender>

    <appender name="infoLog" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="c:/log/info.log"/>
        <param name="maxFileSize" value="2000KB"/>
        <param name="maxBackupIndex" value="10"/>
        <!-- Append : is Add log message blow to the old one trace? -->
        <param name="Append" value="false" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%d{yyyy/MM/dd HH:mm:ss.sss} [%-5p] %c.%M:%L - %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:/log/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:%L - %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:/log/error.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:%L - %m%n"/>
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
                    <param name="LevelMax" value="ERROR" />
                    <param name="LevelMin" value="ERROR" />
            </filter>
    </appender>

    <!-- log all  message in {name} package -->
    <logger name="com.mark.core.test">
        <level value="ALL"/>
         <appender-ref ref="stdout"/>
        <appender-ref ref="traceLog4Debug"/>
        <appender-ref ref="debugLog"/>
        <appender-ref ref="infoLog"/>
        <appender-ref ref="warnLog"/>
        <appender-ref ref="errorLog"/>
    </logger>

   <logger name="com.opensymphony">
        <level value="ERROR" />
        <appender-ref ref="traceLog4Debug"/>
    </logger>

    <logger name="org.apache.struts2">
         <level value="ERROR" />
         <appender-ref ref="traceLog4Debug"/>
    </logger>

    <logger name="org.hibernate.SQL">
        <level value="DEBUG"/>
        <appender-ref ref="traceLog4Debug"/>
        <appender-ref ref="stdout"/>
    </logger>

    <logger name="org.hibernate">
        <level value="error"/>
        <appender-ref ref="traceLog4Debug"/>
        <appender-ref ref="stdout"/>
    </logger>

    <logger name="org.springframework">
        <level value="error"/>
        <appender-ref ref="traceLog4Debug"/>
        <!-- <appender-ref ref="stdout"/>-->
    </logger>

</log4j:configuration>
 

 

package com.mark.core.test;

import org.apache.log4j.Logger;

/**
 * @author mark.lin
 *
 */

public class TestLog4j {

    private static final Logger log = Logger.getLogger(TestLog4j.class);
    /**
     * 测试log4j不同级别日志分文件记录
     *
     */

    public static void main(String[] args) {
        log.debug("debug...");
        log.info("info...xxx");
        log.warn("warn...");
        log.error("error...");

    }

}
 

 

你可能感兴趣的:(log4j)