利用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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值