log4j常用配置


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 ="com.enorth.lichen.common" class ="org.apache.log4j.RollingFileAppender" ><!-- 设置通道ID:com.enorth.lichen和输出方式:org.apache.log4j.RollingFileAppender -->  
     < param name ="File" value ="D:/common.log" /><!-- 设置File参数:日志输出文件名 -->
     < param name ="Append" value ="true" /><!-- 设置是否在重新启动服务时,在原有日志的基础添加新日志 -->
     < param name ="MaxBackupIndex" value ="10" />
     < layout class ="org.apache.log4j.PatternLayout" >
       < param name ="ConversionPattern" value ="%d %p (%c:%L)- %m%n" /><!-- 设置输出文件项目和格式 -->
     </ layout >
   </ appender >
   < appender name ="com.enorth.lichen.special" class ="org.apache.log4j.RollingFileAppender" >
     < param name ="File" value ="D:/special.log" />
     < param name ="Append" value ="true" />
     < param name ="MaxFileSize" value ="102400" /><!-- 设置文件大小100M -->
     < param name ="MaxBackupIndex" value ="10" />
     < layout class ="org.apache.log4j.PatternLayout" >
       < param name ="ConversionPattern" value ="%d %p (%c:%L)- %m%n" />
     </ layout >
   </ appender >
   < logger name ="special.log" ><!-- 设置域名限制,即lichen.log域及以下的日志均输出到下面对应的通道中 -->
     < level value ="info" /><!-- 设置级别 -->
     < appender-ref ref ="com.enorth.lichen.special" /><!-- 与前面的通道id相对应 -->
   </ logger >
   < root ><!-- 设置接收所有输出的通道 -->
     < appender-ref ref ="com.enorth.lichen.common" /><!-- 与前面的通道id相对应 -->
   </ root >
</ log4j:configuration >

程序调用
package com.enorth.lichen.util;

import org.apache.log4j.Logger;

/**
* @author 李晨    
* @version 创建时间:Sep 16, 2009 1:55:46 PM
*/
public class TestLog4jXml {

   public static void main(String[] args) {
//        DOMConfigurator.configure("log4j.xml");//加载.xml文件
//        PropertyConfigurator.configure("E:/study/log4j/log4j.properties");//加载.properties文件
        Logger log=Logger.getLogger( "special.log");
        log.info( "测试测试");
  }
}

日志文件
2009-09-16 14:00:11,140 INFO (special.log:16)- 测试测试

你可能感兴趣的:(职场,休闲)