首页  

logback异步日志配置     所属分类 logback 浏览量 1630
logback AsyncAppender 


	<appender name="ASYN" class="ch.qos.logback.classic.AsyncAppender">
		<discardingThreshold>0</discardingThreshold>
		<queueSize>512</queueSize>
		<appender-ref ref="FILE" />
		<appender-ref ref="FILE2" />
	</appender>



discardingThreshold 丢弃阈值 
queueSize  队列长度


测试代码

import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AsynLogTest {	
	private static final Logger log ;
	static{	
		 System.setProperty("logdir", "/Users/dugang/alllog/asyn/");
		 // final public static String CONFIG_FILE_PROPERTY = "logback.configurationFile";
		 System.setProperty("logback.configurationFile", "logback-asyn.xml");	 
		 log = LoggerFactory.getLogger(AsynLogTest.class);	 
	}

	public static void main(String[] args) throws Exception {		
			int num = 1000000;
			long start = System.currentTimeMillis();
			for(int i=0;i< num;i++){
				log.info(new Date()+"-"+i);
				if(i > 0 && i%10000 ==0){
					System.out.println(System.currentTimeMillis()+":"+i);
				}
			}
			long end =  System.currentTimeMillis();;
			long time = end - start;
			System.out.println(time);
	}
}


System.setProperty("logback.configurationFile", "logback-asyn.xml");
指定logback配置文件 ,默认 为 logback.xml

logback-asyn.xml 完整代码

https://gitee.com/dyyx/logdemo/blob/master/src/main/resources/logback-asyn.xml

 logback初始化过程 

logback 初始化 
ContextInitializer.autoConfig()

Thread [main] (Suspended (breakpoint at line 147 in ContextInitializer))	
	owns: Class (org.slf4j.LoggerFactory) (id=16)	
	ContextInitializer.autoConfig() line: 147	
	StaticLoggerBinder.init() line: 84	
	StaticLoggerBinder.() line: 55	
	LoggerFactory.bind() line: 150	
	LoggerFactory.performInitialization() line: 124	
	LoggerFactory.getILoggerFactory() line: 412	
	LoggerFactory.getLogger(String) line: 357	
	LoggerFactory.getLogger(Class) line: 383	
	AsynLogTest.() line: 13	





AsyncAppender 关键代码 public class AsyncAppender extends AsyncAppenderBase protected boolean isDiscardable(ILoggingEvent event) { Level level = event.getLevel(); return level.toInt() < = Level.INFO_INT; } public static final int DEFAULT_QUEUE_SIZE = 256; int queueSize = DEFAULT_QUEUE_SIZE; int appenderCount = 0; static final int UNDEFINED = -1; if (discardingThreshold == UNDEFINED){ discardingThreshold = queueSize / 5; } blockingQueue = new ArrayBlockingQueue(queueSize); private boolean isQueueBelowDiscardingThreshold() { return (blockingQueue.remainingCapacity() < discardingThreshold); } @Override protected void append(E eventObject) { if (isQueueBelowDiscardingThreshold() && isDiscardable(eventObject)) { return; } preprocess(eventObject); put(eventObject); }

上一篇     下一篇
常用硬件性能参数

System.getenv 与 System.getProperty 的区别

logback.xml变量取不到值问题

A股指数介绍

linux网卡带宽查看

linux cpu信息查看