log4j多级别多输出文件的配置

以下代码需要log4j-1.2.17.jar的支持。

BackUpAppender.java文件内容如下:

package com.shihuan.core.framework.extend;

import org.apache.log4j.Priority;

import org.apache.log4j.RollingFileAppender;

public class BackUpAppender extends RollingFileAppender {

   @Override 

   public boolean isAsSevereAsThreshold(Priority priority) { 

     return this.getThreshold().equals(priority);

   }

}

 log4j.properties文件内容如下:

log4j.rootLogger=debug,debug,info,warn,error,fatal,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender   
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout   
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n
log4j.appender.stdout.layout.ConversionPattern=%-p [%t]: (%F:%L) - %m%n
    
log4j.logger.debug=debug
log4j.appender.debug=com.shihuan.core.framework.extend.BackUpAppender
log4j.appender.debug.layout=org.apache.log4j.PatternLayout   
#log4j.appender.debug.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n
log4j.appender.debug.layout.ConversionPattern=%d\: at %l%n %-p - %m%n
#log4j.appender.debug.datePattern='.'yyyy-MM-dd
log4j.appender.debug.Threshold = debug
log4j.appender.debug.append=true
log4j.appender.debug.File=E:/logs/shihuan_debug.log    

    
log4j.logger.info=info
log4j.appender.info=com.shihuan.core.framework.extend.BackUpAppender
log4j.appender.info.layout=org.apache.log4j.PatternLayout
#log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n
log4j.appender.info.layout.ConversionPattern=%d\: at %l%n %-p - %m%n
#log4j.appender.info.datePattern='.'yyyy-MM-dd
log4j.appender.info.Threshold = info
log4j.appender.info.append=true
log4j.appender.info.File=E:/logs/shihuan_info.log


log4j.logger.warn=warn
log4j.appender.warn=com.shihuan.core.framework.extend.BackUpAppender
log4j.appender.warn.layout=org.apache.log4j.PatternLayout   
#log4j.appender.warn.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n
log4j.appender.warn.layout.ConversionPattern=%d\: at %l%n %-p - %m%n
#log4j.appender.warn.datePattern='.'yyyy-MM-dd
log4j.appender.warn.Threshold = warn
log4j.appender.warn.append=true
log4j.appender.warn.File=E:/logs/shihuan_warn.log 

log4j.logger.error=error
log4j.appender.error=com.shihuan.core.framework.extend.BackUpAppender
log4j.appender.error.layout=org.apache.log4j.PatternLayout   
#log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n
log4j.appender.error.layout.ConversionPattern=%d\: at %l%n %-p - %m%n
#log4j.appender.error.datePattern='.'yyyy-MM-dd  
log4j.appender.error.Threshold = error   
log4j.appender.error.append=true 
log4j.appender.error.File=E:/logs/shihuan_error.log 

log4j.logger.fatal=fatal
log4j.appender.fatal=com.shihuan.core.framework.extend.BackUpAppender
log4j.appender.fatal.layout=org.apache.log4j.PatternLayout   
#log4j.appender.fatal.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n 
log4j.appender.fatal.layout.ConversionPattern=%d\: at %l%n %-p - %m%n
#log4j.appender.fatal.datePattern='.'yyyy-MM-dd   
log4j.appender.fatal.Threshold =fatal   
log4j.appender.fatal.append=true 
log4j.appender.fatal.File=E:/logs/shihuan_fatal.log 

#MAIL
#log4j.appender.mail=org.apache.log4j.net.SMTPAppender
#log4j.appender.mail.Threshold=fatal
#log4j.appender.mail.BufferSize=512
#log4j.appender.mail.SMTPHost=smtp.sina.com
#log4j.appender.mail.Subject=System FATAL ErrorMessage
#log4j.appender.mail.SMTPUsername=shihuan830619
#log4j.appender.mail.SMTPPassword=12345678
#[email protected]
#[email protected]
#log4j.appender.mail.layout=com.lemon.log4j.MyLayOut
##log4j.appender.MAIL.layout.ConversionPattern=[ErrorMessage ] %d - %c -%-4r [%t] %-5p %c %x - %m%n

相关推荐