15.log4j.properties例子:log4j.appender.file.ImmediateFlush

package com.toogen.log4j;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class HelloLog4JToogen {
	// 构造记录器,形参是记录器所在的类,表示要在该类做日志
	private static Logger logger = Logger.getLogger(HelloLog4JToogen.class);
	/** 
	 * @param args
	 */
	public static void main(String[] args) {
		getMessage();
	}
	
	private static void getMessage() {
		// 记录下各种级别的信息,这些信息放在哪儿,以哪种方式存放,在log4j.properties文件中配置.
		if (logger.isDebugEnabled()) {
			logger.debug("This is debug message.");
		}
		if (logger.isInfoEnabled()) {
			logger.info("This is a info message.");
		}
		if (logger.isEnabledFor(Level.WARN)) {
			logger.warn("This is a warn message.");
		}
		if (logger.isEnabledFor(Level.ERROR)) {
			logger.error("This is a error message.....");
		}
	}

}
log4j.rootLogger=debug,file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=Message.log
log4j.appender.file.MaxFileSize=3KB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyyMMdd HH\:mm\:ss} %p %l [%t] %m%n

Message.log(工程目录下)
20120730 14:55:01 DEBUG com.toogen.log4j.HelloLog4JToogen.getMessage(HelloLog4JToogen.java:19) [main] This is debug message.
20120730 14:55:01 INFO com.toogen.log4j.HelloLog4JToogen.getMessage(HelloLog4JToogen.java:22) [main] This is a info message.
20120730 14:55:01 WARN com.toogen.log4j.HelloLog4JToogen.getMessage(HelloLog4JToogen.java:25) [main] This is a warn message.
20120730 14:55:01 ERROR com.toogen.log4j.HelloLog4JToogen.getMessage(HelloLog4JToogen.java:28) [main] This is a error message.....

总结:如果log4j.appender.file.ImmediateFlush=true,意味着输出流会立即输出,不会缓冲,设置为false的话,输出流不会立即输出


你可能感兴趣的:(日志)