你们都很熟悉log4j啦,log5j在log4j的基础上提供了几个改进,应该说是简单和实用的封装。有趣的是log5j主页对本身名字的解释,由于要感谢JDK 1.5,因此才叫了这个名字,不知道是否是升级到JDK1.7之后叫log7j :)java
主页是:http://code.google.com/p/log5j/ 你们能够看看,如今的版本是2.1.2。异步
log5j有几个封装是很是实用的,解释功能的同时咱们稍微看看代码,有些颇有趣的地方:性能
1,不须要指定log所在的类名了ui
原来的用法:this
1 |
private static final Logger log = Logger.getLogger( FeedTask. class ); |
新用法:google
1 |
private static final Logger log = Logger.getLogger(); |
这个能够避免复制的时候忘记改类名。线程
咱们看看代码,Logger里的:debug
1 |
public static Logger getLogger() { |
2 |
return getLogger(getCallerClassName(), true ); |
5 |
private static String getCallerClassName() { |
7 |
return new Exception().getStackTrace()[ 2 ].getClassName(); |
取类名的时候用的是new了一个Exception,直接截取没用反射,简单直接。code
2,支持message的format
原来:
orm
1 |
log.debug( "This thing broke: " + foo + " due to bar: " + bar + " on this thing: " + car ); |
如今:
1 |
log.debug( "This thing broke: %s due to bar: %s on this thing: %s" , foo, bar, car ); |
这个也很方便,效率也提升了,默认用了java.util.Formatter。
咱们看看代码:
LogEvent里的:
1 |
public String message() { |
2 |
if (_params == null || _params.length ==
) { |
6 |
return __tlMessageFormatter.get().format(_formatMessage, _params); |
其中format那部分共了工厂模式,实现的地方是:
01 |
public class DefaultMessageFormatter implements MessageFormatter { |
02 |
private final Formatter _formatter; |
04 |
public DefaultMessageFormatter(Locale locale) { |
07 |
_formatter = new Formatter(locale); |
10 |
public String format(String format, Object... args) { |
11 |
StringBuilder sb = (StringBuilder) _formatter.out(); |
14 |
_formatter.format(format, args); |
3,新接口
能够这样用:
1 |
private static final Log log = LogFactory.getLog(); |
4 |
log.d( "Success! Parameters f=$.10f and i=%d" , f, i); |
5 |
} catch (Exception e) { |
6 |
log.e(e, "Failure! Parameters f=$.10f and i=%d" , f, i); |
7 |
// note: exception passed first separate from parameters |
这个做者也说,仁者见仁,智者见智了。
Log实如今LogImpl里,其实Logger这个类实如今AbstractLoggable,这里有个问题,重复了。
咱们回到主线Logger的调用方式上来吧。
4,能够显式关闭Log。
这样用:
1 |
// initialization phase |
2 |
com.spinn3r.log5j.LogManager.enableExplicitShutdown(); ... |
3 |
// in the very end of the shutdown routine |
4 |
com.spinn3r.log5j.LogManager.shutdown(); |
这个是说若是能够接受log message丢失,而且自己应用程序能够控制自身的初始化和销毁的话,能够用。
这个和log5j的异步log方式有关。
5,性能
简化的写法,看代码:
Logger:
1 |
public void debug(java.lang.Object message) { |
2 |
super .debug(String.valueOf(message)); |
AbstractLoggable:
1 |
public void debug(String formatMessage, Object... params) { |
2 |
if (_logger.isEnabled(LogLevel.DEBUG)) { |
3 |
log(LogEvent.create(_logger, _logName, LogLevel.DEBUG, |
4 |
formatMessage, params)); |
可见,不须要用logger.isDebugEnabled()这样的代码了。
另外log5j是默认使用异步方式的。
主要实如今AsyncLogger里:
01 |
private static final int MAX_QUEUE_SIZE = 100000 ; |
03 |
private static final AtomicLong __errorCounter = new AtomicLong(); |
06 |
private final BlockingQueue<LogEvent> __logEventQueue = |
07 |
new ArrayBlockingQueue<LogEvent>(MAX_QUEUE_SIZE); |
10 |
private final WriterThread _writerThread; |
13 |
public void add(LogEvent event) { |
14 |
if (!__logEventQueue.offer(event)) { |
这个是个生产者消费者模式,WriterThread是消费者。