log4j 配置日志输出(log4j.properties)

时间:2021-05-06 22:04:45

2018年5月27日

一、入门log4j实例

1.1 下载解压log4j.jar(地址:http://mirrors.hust.edu.cn/apache/logging/log4j/2.11.0/

log4j 配置日志输出(log4j.properties)

1.2 新建工程,导入jar包

log4j 配置日志输出(log4j.properties)

1.3 Log4jTest.java:

package test6;

import org.apache.log4j.Logger;
/**
 * @author mmb
 * @function test log4j
 * */
public class Log4jTest {
	
	private static Logger logger = Logger.getLogger(Log4jTest.class);  
	
	public static void main(String [] args){
		
		//BasicConfigurator.configure(); //自动快速地使用缺省Log4j环境。
		
		logger.info("aaaa");
		
		logger.debug("bbbb");
		
		logger.error("cccc");
		
	}
}

1.4 log4j.properties

 ### 设置###
log4j.rootLogger = debug,stdout,D,E

### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

### 输出DEBUG 级别以上的日志到=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 输出ERROR 级别以上的日志到=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://logs/error.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

1.5 输出

log4j 配置日志输出(log4j.properties)


二、log4j 详细配置

参考blog:https://blog.csdn.net/azheng270/article/details/2173430/