利用slf4j+log4j将日志写入指定的文件中

主要实现的是可以从后台传入文件名,写入日志到指定的文件中。(jar包在链接中http://download.csdn.net/download/u010924720/10150019)


代码如下:



package com.test.test;



import com.utils.LogName;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Test01 {
    private static Logger log= LoggerFactory.getLogger(LogName.BUSINESS_LOG);
    private static Logger log1= LoggerFactory.getLogger(LogName.REST_LOG);
    public static void main(String[] args) {

        log.info("请求服务:{}出现异常1", "http:");
        log1.info("请求服务:{}出现异常2", "http:");
        
    }

}


log4j.rootLogger=debug,stdout,D


log4j.appender.stdout = org.apache.log4j.ConsoleAppender     
log4j.appender.stdout.Threshold=trace
log4j.appender.stdout.Target = System.out     
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout     
log4j.appender.stdout.layout.ConversionPattern =  %d{ABSOLUTE} %5p %c{1}:%L - %m%n  
log4j.appender.stdout.Encoding=utf-8

### 輸出到日誌文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E://logs/business/log.log
log4j.appender.D.DatePattern = '.'yyyy-MM-dd'.log'  
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %l -LN:%L] - [ %p ]  %m%n

log4j.logger.business_log=INFO,business_log
log4j.appender.business_log=org.apache.log4j.DailyRollingFileAppender  
log4j.appender.business_log.Append=true  
log4j.appender.business_log.DatePattern='.'yyyy-MM-dd  
log4j.appender.business_log.File=E://logs/business/business_log.log  
log4j.appender.business_log.Threshold=INFO  
log4j.appender.business_log.layout=org.apache.log4j.PatternLayout  
log4j.appender.business_log.layout.ConversionPattern=%d - %c [%t] %-5p %c %x %l - %m%n  
log4j.appender.business_log.Encoding=utf-8

log4j.logger.rest_log=INFO,rest_log
log4j.appender.rest_log=org.apache.log4j.DailyRollingFileAppender  
log4j.appender.rest_log.Append=true  
log4j.appender.rest_log.DatePattern='.'yyyy-MM-dd  
log4j.appender.rest_log.File=E://logs/business/rest_log.log  
log4j.appender.rest_log.Threshold=INFO  
log4j.appender.rest_log.layout=org.apache.log4j.PatternLayout  
log4j.appender.rest_log.layout.ConversionPattern=%d - %c [%t] %-5p %c %x %l - %m%n  
log4j.appender.rest_log.Encoding=utf-8
#log4j.additivity.rest_log=false





你可能感兴趣的:(利用slf4j+log4j将日志写入指定的文件中)