log4j 日志详解

来源:互联网 发布:淘宝上1元秒杀是真的吗 编辑:程序博客网 时间:2024/04/30 10:38

1、下载:

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

2、添加jar到java项目:

log4j-1.2.9.jar


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

  3.1 输出到console端:

<span style="color: rgb(102, 102, 102); "><em># Root logger option</em></span>log4j.rootLogger=INFO, stdout <span style="color: rgb(102, 102, 102); "><em># Direct log messages to stdout</em></span> log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=<span style="font-weight: bold;">%</span>d<span style="font-weight: bold;">{</span>yyyy-MM-dd HH:mm:ss<span style="font-weight: bold;">}</span> <span style="font-weight: bold;">%</span>-5p <span style="font-weight: bold;">%</span>c<span style="font-weight: bold;">{</span>1<span style="font-weight: bold;">}</span>:<span style="font-weight: bold;">%</span>L - <span style="font-weight: bold;">%</span>m<span style="font-weight: bold;">%</span>n
   3.2 输出到日志文件:

<span style="color: rgb(102, 102, 102); "><em># Root logger option</em></span>log4j.rootLogger=INFO, <span style="font-weight: bold;">file</span> <span style="color: rgb(102, 102, 102); "><em># Direct log messages to a log file</em></span>log4j.appender.file=org.apache.log4j.RollingFileAppenderlog4j.appender.file.File=C:\\loging.loglog4j.appender.file.MaxFileSize=1MBlog4j.appender.file.MaxBackupIndex=1log4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=<span style="font-weight: bold;">%</span>d<span style="font-weight: bold;">{</span>yyyy-MM-dd HH:mm:ss<span style="font-weight: bold;">}</span> <span style="font-weight: bold;">%</span>-5p <span style="font-weight: bold;">%</span>c<span style="font-weight: bold;">{</span>1<span style="font-weight: bold;">}</span>:<span style="font-weight: bold;">%</span>L - <span style="font-weight: bold;">%</span>m<span style="font-weight: bold;">%</span>n
  3.3 输出到console同时输出到日志文件:

<span style="color: rgb(102, 102, 102); "><em># Root logger option</em></span>log4j.rootLogger=INFO, <span style="font-weight: bold;">file</span>, stdout <span style="color: rgb(102, 102, 102); "><em># Direct log messages to a log file</em></span>log4j.appender.file=org.apache.log4j.RollingFileAppenderlog4j.appender.file.File=C:\\loging.loglog4j.appender.file.MaxFileSize=1MBlog4j.appender.file.MaxBackupIndex=1log4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=<span style="font-weight: bold;">%</span>d<span style="font-weight: bold;">{</span>yyyy-MM-dd HH:mm:ss<span style="font-weight: bold;">}</span> <span style="font-weight: bold;">%</span>-5p <span style="font-weight: bold;">%</span>c<span style="font-weight: bold;">{</span>1<span style="font-weight: bold;">}</span>:<span style="font-weight: bold;">%</span>L - <span style="font-weight: bold;">%</span>m<span style="font-weight: bold;">%</span>n <span style="color: rgb(102, 102, 102); "><em># Direct log messages to stdout</em></span>log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=<span style="font-weight: bold;">%</span>d<span style="font-weight: bold;">{</span>yyyy-MM-dd HH:mm:ss<span style="font-weight: bold;">}</span> <span style="font-weight: bold;">%</span>-5p <span style="font-weight: bold;">%</span>c<span style="font-weight: bold;">{</span>1<span style="font-weight: bold;">}</span>:<span style="font-weight: bold;">%</span>L - <span style="font-weight: bold;">%</span>m<span style="font-weight: bold;">%</span>n
  3.4  按天roll over(翻滚,转存)日志:

<span style="color: rgb(102, 102, 102); "><em></em></span># Root logger optionlog4j.rootLogger=INFO,console,<span style="background-color: rgb(51, 255, 51);">dailyFile</span>##Daily log:log4j.appender.dailyFile=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.dailyFile.File=e:/ftplog/autoftp.loglog4j.appender.dailyFile.Append=truelog4j.appender.dailyFile.layout=org.apache.log4j.PatternLayout  log4j.appender.dailyFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%nlog4j.appender.dailyFile.Threshold=INFO<span style="color: rgb(51, 255, 51);"><span style="background-color: rgb(51, 255, 51);"><span style="color: rgb(0, 0, 0);">log4j.appender.dailyFile.DatePattern='.'yyyy-MM-dd</span></span> </span> # Direct log messages to consolelog4j.appender.console=org.apache.log4j.ConsoleAppenderlog4j.appender.console.Threshold=INFOlog4j.appender.console.ImmediateFlush=truelog4j.appender.console.Target=System.outlog4j.appender.console.layout=org.apache.log4j.PatternLayoutlog4j.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-MMRoll over at the end of each month and the beginning of the next month.'.' yyyy-MM-ddThis is the default value and roll over at midnight each day.'.' yyyy-MM-dd-aRoll over at midday and midnight of each day.'.' yyyy-MM-dd-HHRoll over at the top of every hour.'.' yyyy-MM-dd-HH-mmRoll over every minute.'.' yyyy-wwRoll over on the first day of each week depending upon the locale.

 4、测试代码如下:

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. import org.apache.log4j.Logger;  
  2.     import org.apache.log4j.BasicConfigurator;  
  3.       
  4.     public class test{  
  5.       
  6.       static Logger logger = Logger.getLogger(Test.class);  
  7.       
  8.       public  
  9.       static  
  10.       void main(String argv[]) {  
  11.         BasicConfigurator.configure();  
  12.         logger.debug("test This");  
  13.         logger.info("This is a info.");  
  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.

0 0
原创粉丝点击