Log4j入门教程

在maven仓库搜索log4j配置  https://mvnrepository.com/tags/maven

 

 

 

 1 log4j.rootLogger=DEBUG, Console ,File
 2 
 3 #Console
 4 log4j.appender.Console=org.apache.log4j.ConsoleAppender  
 5 log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
 6 log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
 7 #File
 8 log4j.appender.File = org.apache.log4j.FileAppender
 9 log4j.appender.File.File = F://log.log
10 log4j.appender.File.layout = org.apache.log4j.PatternLayout
11 log4j.appender.File.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n
配置文件

 

 

 

 

 1 package com.uos.log4j;
 2 
 3 import org.apache.log4j.Logger;
 4 
 5 
 6 public class Log4jTest {
 7 
 8     private static Logger logger = Logger.getLogger(Log4jTest.class);
 9     public static void main(String[] args) {
10         logger.fatal("严重错误fatal信息");
11         logger.error("报错error信息");
12         logger.warn("警告warn信息");
13         logger.debug("调试debug信息");
14         logger.info("普通info信息");
15         logger.error("报错信息", new IllegalArgumentException("非法参数"));
16     }
17 }
测试代码

 

 

 

 

 

 

 1 log4j.rootLogger=DEBUG, Console ,File,DailyRollingFile,RollingFile
 2 
 3 #Console
 4 log4j.appender.Console=org.apache.log4j.ConsoleAppender  
 5 log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
 6 log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
 7 #File
 8 log4j.appender.File = org.apache.log4j.FileAppender
 9 log4j.appender.File.File = F://log2.log
10 log4j.appender.File.layout = org.apache.log4j.PatternLayout
11 log4j.appender.File.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n
12 
13 
14 #DailyRollingFile
15 log4j.appender.DailyRollingFile = org.apache.log4j.DailyRollingFileAppender
16 log4j.appender.DailyRollingFile.File = F://log3.log
17 log4j.appender.DailyRollingFile.layout = org.apache.log4j.PatternLayout
18 log4j.appender.DailyRollingFile.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n
19 
20 #RollingFile
21 log4j.appender.RollingFile = org.apache.log4j.RollingFileAppender
22 log4j.appender.RollingFile.File = F://log4.log
23 log4j.appender.RollingFile.MaxFileSize=1KB
24 log4j.appender.RollingFile.MaxBackupIndex=3
25 log4j.appender.RollingFile.layout = org.apache.log4j.PatternLayout
26 log4j.appender.RollingFile.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n
配置文件代码

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2019-11-14 22:21  红尘年少  阅读(143)  评论(0编辑  收藏  举报