日志工具类v1.0

这里用的是Log4j-1.2.15


下面是日志工具类v1.0

package com.jadyer.util;

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

/**
 * 日志工具类
 * @see ===============================================================================================================
 * @see 【用法如下】
 * @see 说明:在想要记录日志的位置,执行LogUtil.setXxxLogger(),然后就可以LogUtil.getLogger().info("....")
 * @see 举例:假设一个SSH2工程中有个名为userLogin()的Action方法,其内部调用了UserService,UserService内部又调用了UserDAO
 * @see      我们在userLogin()、UserService、UserDAO中均使用了LogUtil.getLogger().info("....")记录相应的日志信息
 * @see      这时,我们的日志信息,会被自动记录到ecpaycus-default.log文件中
 * @see      同理,我们若在userLogin()方法的第一句话执行LogUtil.setWebCusLogger()
 * @see           则日志会被自动写入ecpaycus-webcus.log文件中,而不会写入到ecpaycus-default.log中了
 * @see ===============================================================================================================
 * @author  http://blog.csdn.net/jadyer
 * @version v1.0
 * @editime Apr 28, 2012 2:10:45 PM
 * @created Dec 27, 2011 12:03:01 PM
 */
public class LogUtil {
	//自定义线程范围内共享的对象。即它会针对不同线程分别创建独立的对象
	//此时每个线程得到的将是自己的实例,各线程间得到的实例没有任何关联
	private static ThreadLocal<Log> currentLoggerMap = new ThreadLocal<Log>();
	
	/**web定制日志记录器*/
	private static Log webCusLogger = LogFactory.getLog("ecpaycusWebCusLogger");
	/**默认日志记录器*/
	private static Log defaultLogger = LogFactory.getLog("ecpaycusDefaultLogger");
	/**对账文件日志记录器*/
	private static Log recFileLogger = LogFactory.getLog("ecpaycusRecFileLogger");
	
	private LogUtil(){}

	/**
	 * 获取当前线程中的日志记录器
	 * @author 宏宇
	 * @create May 2, 2012 1:40:49 PM
	 */
	public static Log getLogger() {
		Log log = currentLoggerMap.get();
		if(null == log){
			return defaultLogger;
		}else{
			return log;
		}
	}
	
	/**
	 * Web定制日志记录器
	 * @author 宏宇
	 * @create Apr 28, 2012 2:10:45 PM
	 */
	public static void setWebCusLogger(){
		//每个线程调用全局的ThreadLocal.set()方法
		//相当于在其内部的Map中增加一条记录,key是各自的线程,value是各自set()的值
		//取的时候,直接ThreadLocal.get()即可。。。。。。。。。至于其内部实现,你懂的
		//我博客里对此有记载:http://blog.csdn.net/jadyer/article/details/7338071
		//Struts2就是这么做的,请见com.opensymphony.xwork2.ActionContex第43和166行源码
		currentLoggerMap.set(webCusLogger);
	}
	
	/**
	 * 默认日志记录器
	 * @author http://blog.csdn/net/jadyer
	 * @create May 17, 2012 5:14:02 PM
	 */
	public static void setDefaultLogger(){
		currentLoggerMap.set(defaultLogger);
	}
	
	/**
	 * 对账文件日志记录器
	 * @author 宏宇
	 * @create Apr 28, 2012 2:17:10 PM
	 */
	public static void setRecFileLogger(){
		currentLoggerMap.set(recFileLogger);
	}
}

下面是Log4j的配置文件log4j.properties

#use Root
log4j.rootLogger=DEBUG,CONSOLE
log4j.logger.ecpaycusWebCusLogger=DEBUG,ECPAYCUS_WEBCUS_LOG
log4j.logger.ecpaycusDefaultLogger=DEBUG,ECPAYCUS_DEFAULT_LOG
log4j.logger.ecpaycusRecFileLogger=DEBUG,ECPAYCUS_RECFILE_LOG
log4j.additivity.ecpaycusWebCusLogger=true
log4j.additivity.ecpaycusDefaultLogger=true
log4j.additivity.ecpaycusRecFileLogger=true

#use Console
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=DEBUG
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
#log4j.appender.CONSOLE.layout.ConversionPattern=[%d{yyyyMMdd HH:mm:ss} %p] %F.%M()\u2605\u2605%m%n
#log4j.appender.CONSOLE.layout.ConversionPattern=[%d{yyyyMMdd HH:mm:ss}][%t][%F.%M]%m%n
#log4j.appender.CONSOLE.layout.ConversionPattern=[%d{yyyyMMdd HH:mm:ss}][%t][%C.%M]%m%n
log4j.appender.CONSOLE.layout.ConversionPattern=[%d{yyyyMMdd HH:mm:ss}][%t][%C{1}.%M]%m%n

#use DailyRollFile for WebCus
log4j.appender.ECPAYCUS_WEBCUS_LOG=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ECPAYCUS_WEBCUS_LOG.File=../logs/ecpaycus-webcus.log
log4j.appender.ECPAYCUS_WEBCUS_LOG.encoding=UTF-8
log4j.appender.ECPAYCUS_WEBCUS_LOG.Threshold=DEBUG
log4j.appender.ECPAYCUS_WEBCUS_LOG.DatePattern='.'yyyyMMdd
log4j.appender.ECPAYCUS_WEBCUS_LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.ECPAYCUS_WEBCUS_LOG.layout.ConversionPattern=[%d{yyyyMMdd HH:mm:ss} %p] %F.%M()\u2605\u2605%m%n

#use DailyRollFile for Defaule LogFile
log4j.appender.ECPAYCUS_DEFAULT_LOG=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ECPAYCUS_DEFAULT_LOG.File=../logs/ecpaycus-default.log
log4j.appender.ECPAYCUS_DEFAULT_LOG.encoding=UTF-8
log4j.appender.ECPAYCUS_DEFAULT_LOG.Threshold=DEBUG
log4j.appender.ECPAYCUS_DEFAULT_LOG.DatePattern='.'yyyyMMdd
log4j.appender.ECPAYCUS_DEFAULT_LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.ECPAYCUS_DEFAULT_LOG.layout.ConversionPattern=[%d{yyyyMMdd HH:mm:ss} %p] %F.%M()\u2605\u2605%m%n

#use DailyRollFile for ReconciliationFile
log4j.appender.ECPAYCUS_RECFILE_LOG=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ECPAYCUS_RECFILE_LOG.File=../logs/ecpaycus-recfile.log
log4j.appender.ECPAYCUS_RECFILE_LOG.encoding=UTF-8
log4j.appender.ECPAYCUS_RECFILE_LOG.Threshold=DEBUG
log4j.appender.ECPAYCUS_RECFILE_LOG.DatePattern='.'yyyyMMdd
log4j.appender.ECPAYCUS_RECFILE_LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.ECPAYCUS_RECFILE_LOG.layout.ConversionPattern=[%d{yyyyMMdd HH:mm:ss} %p] %F.%M()\u2605\u2605%m%n

你可能感兴趣的:(日志工具类v1.0)