log4j配置的相关例子

一:使用log4j.xml

1.log4j.xml配置

<?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="/n[%r][%p][%t] %d{HH:mm:ss,SSS} method:%l/n%m%n"/>
   </layout>
</appender>

<appender name="DEBUG" class="org.apache.log4j.DailyRollingFileAppender">
   <param name="File" value="./logs/app-debug.log"/>
   <param name="DatePattern" value="'.'yyyy-MM-dd"/>
   <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss},%m%n"/>
   </layout>
   <filter class="org.apache.log4j.varia.LevelRangeFilter">
    <param name="LevelMin" value="DEBUG"/>
    <param name="LevelMax" value="DEBUG"/>
   </filter>
</appender>

<appender name="INFO" class="org.apache.log4j.DailyRollingFileAppender">
   <param name="File" value="./logs/app-info.log"/>
   <param name="DatePattern" value="'.'yyyy-MM-dd"/>
   <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="[%-5r][%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l %m%n"/>
   </layout>
   <filter class="org.apache.log4j.varia.LevelRangeFilter">
    <param name="LevelMin" value="INFO"/>
    <param name="LevelMax" value="INFO"/>
   </filter>
</appender>

<appender name="WARN" class="org.apache.log4j.DailyRollingFileAppender">
   <param name="File" value="./logs/app-warn.log"/>
   <param name="DatePattern" value="'.'yyyy-MM-dd"/>
   <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="[%-5r][%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l %m%n"/>
   </layout>
   <filter class="org.apache.log4j.varia.LevelRangeFilter">
    <param name="LevelMin" value="WARN"/>
    <param name="LevelMax" value="WARN"/>
   </filter>
</appender>

<appender name="ERROR" class="org.apache.log4j.DailyRollingFileAppender">
   <param name="File" value="./logs/app-error.log"/>
   <param name="DatePattern" value="'.'yyyy-MM-dd"/>
   <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="[%-5r][%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l %m%n"/>
   </layout>
   <filter class="org.apache.log4j.varia.LevelRangeFilter">
    <param name="LevelMin" value="ERROR"/>
    <param name="LevelMax" value="ERROR"/>
   </filter>
</appender>

<root>
   <priority value="STDOUT"/>
   <appender-ref ref="STDOUT"/>
   <appender-ref ref="DEBUG"/>
   <appender-ref ref="INFO"/>
   <appender-ref ref="WARN"/>
   <appender-ref ref="ERROR"/>
</root>

</log4j:configuration>

2.在类中使用

package com.tianze.jmsn.test;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
public class TestLogger {
    static Logger logger = Logger.getLogger(TestLogger.class.getName());
    public static void main(String[] s){
           DOMConfigurator.configureAndWatch("D://log4jcfg.xml");
            logger.debug("----logger debug ----");
            logger.info("----logger info ----");
            logger.warn("----logger warn ----");
            logger.error("----logger errors ----");
            logger.fatal("----logger fatal ----");
//        logger.log(Level.DEBUG, "----logger debug ----");
//        logger.log(Level.INFO, "----logger info ----");
//        logger.log(Level.WARN, "----logger warn ----");
//        logger.log(Level.ERROR, "----logger error ----");
//        logger.log(Level.FATAL, "----logger fatal ----");
    }
}

二:使用log4j.properties

1.log4j.properties配置

    将配置好的属性文件放到src下。

# To change this template, choose Tools | Templates
# and open the template in the editor.
log4j.rootLogger=DEBUG,STDOUT,DEBUG,INFO,WARN,ERROR

# STDOUT is set to be a ConsoleAppender.
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
# STDOUT uses PatternLayout.
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=/n[%r][%p][%t] %d{HH:mm:ss,SSS} method:%l/n%m%n
log4j.logger.com.tianze.jmsn.test=DEBUG

# DEBUG is set to be a DailyRollingFileAppender.
log4j.appender.DEBUG=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DEBUG.File=./logs/app-debug.log
log4j.appender.DEBUG.DatePattern='.'yyyy-MM-dd
# DEBUG uses PatternLayout.
log4j.appender.DEBUG.layout=org.apache.log4j.PatternLayout
log4j.appender.DEBUG.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss},%m%n
#log4j.appender.DEBUG.filter=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.DEBUG.filter.LevelMin=DEBUG
log4j.appender.DEBUG.filter.LevelMax=DEBUG

# INFO is set to be a DailyRollingFileAppender.
log4j.appender.INFO=org.apache.log4j.DailyRollingFileAppender
log4j.appender.INFO.File=./logs/app-info.log
log4j.appender.INFO.DatePattern='.'yyyy-MM-dd
# INFO uses PatternLayout.
log4j.appender.INFO.layout=org.apache.log4j.PatternLayout
log4j.appender.INFO.layout.ConversionPattern=[%-5r][%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l %m%n
#log4j.appender.INFO.filter=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.INFO.filter.LevelMin=INFO
log4j.appender.INFO.filter.LevelMax=INFO

# WARN is set to be a DailyRollingFileAppender.
log4j.appender.WARN=org.apache.log4j.DailyRollingFileAppender
log4j.appender.WARN.File=./logs/app-warn.log
log4j.appender.WARN.DatePattern='.'yyyy-MM-dd
# INFO uses PatternLayout.
log4j.appender.WARN.layout=org.apache.log4j.PatternLayout
log4j.appender.WARN.layout.ConversionPattern=[%-5r][%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l %m%n
#log4j.appender.WARN.filter=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.WARN.filter.LevelMin=WARN
log4j.appender.WARN.filter.LevelMax=WARN

# ERROR is set to be a DailyRollingFileAppender.
log4j.appender.ERROR=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERROR.File=./logs/app-error.log
log4j.appender.ERROR.DatePattern='.'yyyy-MM-dd
# INFO uses PatternLayout.
log4j.appender.ERROR.layout=org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.ConversionPattern=[%-5r][%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l %m%n
#log4j.appender.ERROR.filter=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.ERROR.filter.LevelMin=ERROR
log4j.appender.ERROR.filter.LevelMax=ERROR

2.在类中使用Log4j

package com.tianze.jmsn.test;
import org.apache.log4j.Logger;
public class TestLogger {
    static Logger logger = Logger.getLogger(TestLogger.class.getName());
    public static void main(String[] s){
            logger.debug("----logger debug ----");
            logger.info("----logger info ----");
            logger.warn("----logger warn ----");
            logger.error("----logger errors ----");
            logger.fatal("----logger fatal ----");
//        logger.log(Level.DEBUG, "----logger debug ----");
//        logger.log(Level.INFO, "----logger info ----");
//        logger.log(Level.WARN, "----logger warn ----");
//        logger.log(Level.ERROR, "----logger error ----");
//        logger.log(Level.FATAL, "----logger fatal ----");
    }
}

你可能感兴趣的:(log4j)