self4j log4j配置与调用

slf4j-api定义了日志的接口,可以使用不同的日志类实现接口,此处以log4j 为例:


引入slf4j-api,slf4j-log4j12 jar包


		
			org.slf4j
			slf4j-api
			1.7.25
		

		
		
			org.slf4j
			slf4j-log4j12
			1.7.25
			test
		
添加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 ###
### 按日期org.apache.log4j.DailyRollingFileAppender 
### 按文件大小org.apache.log4j.DailyRollingFileAppender   
log4j.appender.D =org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/log.log
### log4j.appender.D.MaxFileSize=1KB
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

### 输出ERROR 级别以上的日志到=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =logs/error.log
### log4j.appender.E.MaxFileSize=1KB 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
添加测试代码

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class SelfLog4jTest {
private static Logger logger = LoggerFactory.getLogger(SelfLog4jTest.class);


public static void main(String[] args) {
for(int i=0;i<10;i++){
// 记录debug级别的信息
logger.debug("This is debug message.");
// 记录info级别的信息
logger.info("This is info message.");
// 记录error级别的信息
logger.error("This is error message.");
// 记录error级别的信息
logger.error("This is error message,{}---{}.","admin","123");
}
}
}


你可能感兴趣的:(self4j log4j配置与调用)