如何配置使用 Log4j

1、下载:

http://archive.apache.org/dist/logging/log4j/

2、添加jar到java项目:

 

3、创建:Log4j.Properties文件,放在src文件夹下即可:

  3.1 输出到console端:

 

# Root logger option
log4j.rootLogger=INFO, stdout
 
# Direct log messages to stdout 
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=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

   3.2 输出到日志文件:

 

 

# Root logger option
log4j.rootLogger=INFO, file
 
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

  3.3 输出到console同时输出到日志文件:

 

 

# Root logger option
log4j.rootLogger=INFO, file, stdout
 
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
 
# Direct log messages to stdout
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=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

  3.4  按天roll over(翻滚,转存)日志:

 

 

# Root logger option
log4j.rootLogger=INFO,console,dailyFile
##Daily log:
log4j.appender.dailyFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyFile.File=e:/ftplog/autoftp.log
log4j.appender.dailyFile.Append=true
log4j.appender.dailyFile.layout=org.apache.log4j.PatternLayout  
log4j.appender.dailyFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n
log4j.appender.dailyFile.Threshold=INFO
log4j.appender.dailyFile.DatePattern='.'yyyy-MM-dd  

# Direct log messages to console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=INFO
log4j.appender.console.ImmediateFlush=true
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

 PS:

Debug Level:  日志记录等级

 

We have used DEBUG with both the appenders. All the possible options are:

  • TRACE

  • DEBUG

  • INFO

  • WARN

  • ERROR

  • FATAL

  • ALL

Daily Log File Generation: 每日文件生成模式

 

DatePatternDescription
'.' yyyy-MM Roll over at the end of each month and the beginning of the next month.
'.' yyyy-MM-dd This is the default value and roll over at midnight each day.
'.' yyyy-MM-dd-a Roll over at midday and midnight of each day.
'.' yyyy-MM-dd-HH Roll over at the top of every hour.
'.' yyyy-MM-dd-HH-mm Roll over every minute.
'.' yyyy-ww Roll over on the first day of each week depending upon the locale.

 

 

 4、测试代码如下:

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. import org.apache.log4j.Logger;  
  2.     import org.apache.log4j.BasicConfigurator;  
  3.       
  4.     public class Hello {  
  5.       
  6.       static Logger logger = Logger.getLogger(Hello.class);  
  7.       
  8.       public  
  9.       static  
  10.       void main(String argv[]) {  
  11.         BasicConfigurator.configure();  
  12.         logger.debug("Hello world.");  
  13.         logger.info("What a beatiful day.");  
  14.       }  
  15.     }  

Output:

2013-12-14 13:02:06 ERROR Hello:12 - Hello world.
2013-12-14 13:02:06 INFO  Hello:13 - What a beatiful day.

 

----------------------------------------

Dylan   presents.

posted @ 2016-06-22 08:50  华行天下  阅读(536)  评论(0编辑  收藏  举报