日志组件LO4J

第一步:引入jar包 目前已经不会更新了。

log4j-1.2.17.jar

第二步:引入配置文件log4j.properties

log4j.properties

如图:

 1 ### 设置根logger###
 2 log4j.rootLogger = debug,stdout,D,E
 3 
 4 ### 输出信息到控制台###
 5 log4j.appender.stdout = org.apache.log4j.ConsoleAppender
 6 log4j.appender.stdout.Target = System.out
 7 log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
 8 log4j.appender.stdout.layout.ConversionPattern = [%d{yyyyMMdd HH:mm:ssSSS\}%-5p]{%m} %l%n
 9 
10 ### 输出DEBUG 级别以上的日志到=E://logs/error.log ###
11 log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
12 log4j.appender.D.File = E://logs/debug.log
13 log4j.appender.D.Append = true
14 log4j.appender.D.layout = org.apache.log4j.PatternLayout
15 log4j.appender.D.layout.ConversionPattern = [%d{yyyyMMdd HH:mm:ssSSS\}%-5p]{%m} %l%n
16 log4j.appender.D.filter.F=org.apache.log4j.varia.LevelRangeFilter
17 log4j.appender.D.filter.F.LevelMin=DEBUG
18 log4j.appender.D.filter.F.LevelMax=DEBUG
19 
20 ### 输出ERROR 级别以上的日志到=E://logs/error.log ###
21 log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
22 log4j.appender.E.File =E://logs/error.log 
23 log4j.appender.E.Append = true
24 log4j.appender.E.layout = org.apache.log4j.PatternLayout
25 log4j.appender.E.layout.ConversionPattern = [%d{yyyyMMdd HH:mm:ssSSS\}%-5p]{%m} %l%n
26 log4j.appender.E.filter.F=org.apache.log4j.varia.LevelRangeFilter
27 log4j.appender.E.filter.F.LevelMin=ERROR
28 log4j.appender.E.filter.F.LevelMax=ERROR

第三步:java代码中体现

 1 package cd.com.log4j;
 2 
 3 import org.apache.log4j.Logger;
 4 
 5 public class App 
 6 {
 7     private static Logger logger = Logger.getLogger(App.class);
 8     
 9     public static void main( String[] args )
10     {
11         //日志记录
12         logger.debug("debug: hello log4j");
13         logger.info("info: hello log4j");
14         logger.warn("warn: hello log4j");
15         logger.error("error: hello log4j");
16     }
17 }

控制台显示:

[20170524 10:38:02401DEBUG]{debug: hello log4j} cd.com.log4j.App.main(App.java:12)
[20170524 10:38:02401INFO ]{info: hello log4j} cd.com.log4j.App.main(App.java:13)
[20170524 10:38:02401WARN ]{warn: hello log4j} cd.com.log4j.App.main(App.java:14)
[20170524 10:38:02401ERROR]{error: hello log4j} cd.com.log4j.App.main(App.java:15)

 

posted @ 2019-06-12 16:05  土木转行的人才  阅读(155)  评论(0编辑  收藏  举报