首页 > 其他 > 详细

log4j注意事项

时间:2015-10-10 12:18:43      阅读:233      评论:0      收藏:0      [点我收藏+]

   1.不应该大范围,跨多线程共用loger对象。

代码1-1
public
void callAppenders(LoggingEvent event) { int writes = 0; for (Category c = this; c != null; c = parent) { synchronized (c) { if (aai != null) { writes += aai.appendLoopOnAppenders(event); } if (!additive) { break; } } } if (writes == 0) { repository.emitNoAppenderWarning(this); } }
代码1-1截取自Logger对象的父类Category类。可以看到其使用logger对象作为锁,如果大范围跨线程使用相同logger对象会造成大范围阻塞。

2.如果使用同步日志也不应该大范围跨线程使用相同appender对象。

出于outputstream的write方法的锁的使用。

3.如果使用异步日志也不应该大范围跨线程使用相同的AsyncAppender对象。

代码1-2
public
void append(final LoggingEvent event) { // // if dispatcher thread has died then // append subsequent events synchronously // See bug 23021 if ((dispatcher == null) || !dispatcher.isAlive() || (bufferSize <= 0)) { synchronized (appenders) { appenders.appendLoopOnAppenders(event); } return; } // Set the NDC and thread name for the calling thread as these // LoggingEvent fields were not set at event creation time. event.getNDC(); event.getThreadName(); // Get a copy of this thread‘s MDC. event.getMDCCopy(); if (locationInfo) { event.getLocationInformation(); } synchronized (buffer) { while (true) { int previousSize = buffer.size(); if (previousSize < bufferSize) { buffer.add(event); // // if buffer had been empty // signal all threads waiting on buffer // to check their conditions. // if (previousSize == 0) { buffer.notifyAll(); } break; } // // Following code is only reachable if buffer is full // // // if blocking and thread is not already interrupted // and not the dispatcher then // wait for a buffer notification boolean discard = true; if (blocking && !Thread.interrupted() && Thread.currentThread() != dispatcher) { try { buffer.wait(); discard = false; } catch (InterruptedException e) { // // reset interrupt status so // calling code can see interrupt on // their next wait or sleep. Thread.currentThread().interrupt(); } } // // if blocking is false or thread has been interrupted // add event to discard map. // if (discard) { String loggerName = event.getLoggerName(); DiscardSummary summary = (DiscardSummary) discardMap.get(loggerName); if (summary == null) { summary = new DiscardSummary(event); discardMap.put(loggerName, summary); } else { summary.add(event); } break; } } } }

代码1-2取自AsyncAppender,可以看到使用AsyncAppender对象的buffer作为锁。

 
 

log4j注意事项

原文:http://www.cnblogs.com/barker/p/4866066.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!