Log4J集成commons-logging

1.创建commons-logging的配置文件commons-logging.properties置于代码的根目录下,并指定使用Log4J的Logger

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

 

2.创建Log4J的配置文件log4j.properties置于代码的根目录下

#using log4j 1.2.x
#root Logger for all class
log4j.rootLogger=DEBUG,console,logfile
#Logger for specific class
#log4j.logger.common.Log4J=DEBUG,console,logfile

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.Encoding=GBK
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%F] [%t] [line %L in %c] - %m%n

#this type of Appender will create log file everyday
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.DatePattern='.'yyyy-MM-dd
log4j.appender.logfile.File=logs/testLog.log
log4j.appender.logfile.Append=true
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%F] [%t] [%L] [%c] - %m%n

# %d = time
# %p = level
# %F = class name
# %t = method name
# %L = line number in class
# %c = class name(include package)
# %m = message
# %n = new line

 

3.测试代码

package common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Log4J {
	
	private static Log logger = LogFactory.getLog(Log4J.class);   

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(logger.isInfoEnabled())logger.info("this is message");
		logger.error("error");
		System.out.println("hello");
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值