本文主要是介绍java Logger类和Apache slf4j,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 相关jar包
下载地址
2 定义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 = D://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 =D://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
3 Demo
工程文件结构如下:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;public class Test {private static Logger logger = LoggerFactory.getLogger(Test.class);public static void main(String[] args) {// System.out.println("This is println message.");// 记录debug级别的信息logger.debug("This is debug message.");// 记录info级别的信息logger.info("This is info message.");// 记录error级别的信息logger.error("This is error message.");}
}
这篇关于java Logger类和Apache slf4j的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!