log4j learning

 1. Introduction to Log4j

Log4j has three main components: Loggers (loggers), Appenders (output sources) and Layouts (layouts). This can be simply understood as the log category, where the log is to be output and in what form the log is output. Using these three components together makes it easy to log the type and level of information, and to control the style and location of log output at runtime.

 

1、Loggers

Loggers components are divided into five levels in this system: DEBUG, INFO, WARN, ERROR and FATAL. These five levels are in order, DEBUG < INFO < WARN < ERROR < FATAL, which are used to specify the importance of this log information. It is very important to understand this. Log4j has a rule: only the output level is not lower than the setting If the Loggers level is set to INFO, the log information of INFO, WARN, ERROR, and FATAL levels will be output, while DEBUG with a lower level than INFO will not be output.

 

2. Appenders
disabling and using log requests are only the basic functions of Log4j. The Log4j logging system also provides many powerful functions, such as allowing logs to be output to different places, such as console (Console), files (Files), etc., can be based on the number of days Or the file size generates a new file that can be streamed to other places, etc.

Commonly used classes are as follows:
org.apache.log4j.ConsoleAppender (console)
org.apache.log4j.FileAppender (file)
org.apache.log4j.DailyRollingFileAppender (generates a log file every day)
org.apache.log4j.RollingFileAppender (file When the size reaches the specified size, a new file is generated)
org.apache.log4j.WriterAppender (send log information to any specified place in stream format)

3. Layouts

Sometimes users want to format their log output according to their own preferences. Log4j can append Layouts to Appenders to complete this function. Layouts provides four log output styles, such as HTML style, freely specified style, style containing log level and information, and style containing log time, thread, category and other information.

Commonly used classes are as follows:

org.apache.log4j.HTMLLayout (layout in HTML form)
org.apache.log4j.PatternLayout (layout pattern can be flexibly specified)
org.apache.log4j.SimpleLayout (contains the level and information of log information String)
org.apache.log4j.TTCCLayout (including log generation time, thread, category and other information)

Second, the configuration details

In practical applications, to make Log4j run in the system, the configuration file must be set in advance. The configuration file actually sets the Logger, Appender and Layout accordingly. Log4j supports two configuration file formats, one is an XML format file and the other is a properties property file. The following uses the properties property file as an example to introduce the configuration of log4j.properties.

1、配置根Logger:
log4j.rootLogger = [ level ] , appenderName1, appenderName2, …
log4j.additivity.org.apache=false:表示Logger不会在父Logger的appender里输出,默认为true。
level :设定日志记录的最低级别,可设的值有OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL或者自定义的级别,Log4j建议只使用中间四个级别。通过在这里设定级别,您可以控制应用程序中相应级别的日志信息的开关,比如在这里设定了INFO级别,则应用程序中所有DEBUG级别的日志信息将不会被打印出来。
appenderName:就是指定日志信息要输出到哪里。可以同时指定多个输出目的地,用逗号隔开。
例如:log4j.rootLogger=INFO,A1,B2,C3

2、配置日志信息输出目的地(appender):
log4j.appender.appenderName = className
appenderName:自定义appderName,在log4j.rootLogger设置中使用;
className:可设值如下:
(1)org.apache.log4j.ConsoleAppender(控制台)
(2)org.apache.log4j.FileAppender(文件)
(3)org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)
(4)org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件)
(5)org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)
(1)ConsoleAppender选项:
Threshold=WARN:指定日志信息的最低输出级别,默认为DEBUG。
ImmediateFlush=true:表示所有消息都会被立即输出,设为false则不输出,默认值是true。
Target=System.err:默认值是System.out。
(2)FileAppender选项:
Threshold=WARN:指定日志信息的最低输出级别,默认为DEBUG。
ImmediateFlush=true:表示所有消息都会被立即输出,设为false则不输出,默认值是true。
Append=false:true表示消息增加到指定文件中,false则将消息覆盖指定的文件内容,默认值是true。
File=D:/logs/logging.log4j:指定消息输出到logging.log4j文件中。
(3)DailyRollingFileAppender选项:
Threshold=WARN:指定日志信息的最低输出级别,默认为DEBUG。
ImmediateFlush=true:表示所有消息都会被立即输出,设为false则不输出,默认值是true。
Append=false:true表示消息增加到指定文件中,false则将消息覆盖指定的文件内容,默认值是true。
File=D:/logs/logging.log4j:指定当前消息输出到logging.log4j文件中。
DatePattern='.'yyyy-MM:每月滚动一次日志文件,即每月产生一个新的日志文件。当前月的日志文件名为logging.log4j,前一个月的日志文件名为logging.log4j.yyyy-MM。
另外,也可以指定按周、天、时、分等来滚动日志文件,对应的格式如下:
1)'.'yyyy-MM:每月
2)'.'yyyy-ww:每周
3)'.'yyyy-MM-dd:每天
4)'.'yyyy-MM-dd-a:每天两次
5)'.'yyyy-MM-dd-HH:每小时
6)'.'yyyy-MM-dd-HH-mm:每分钟
(4)RollingFileAppender选项:
Threshold=WARN:指定日志信息的最低输出级别,默认为DEBUG。
ImmediateFlush=true:表示所有消息都会被立即输出,设为false则不输出,默认值是true。
Append=false:true表示消息增加到指定文件中,false则将消息覆盖指定的文件内容,默认值是true。
File=D:/logs/logging.log4j:指定消息输出到logging.log4j文件中。
MaxFileSize=100KB:后缀可以是KB, MB 或者GB。在日志文件到达该大小时,将会自动滚动,即将原来的内容移到logging.log4j.1文件中。
MaxBackupIndex=2:指定可以产生的滚动文件的最大数,例如,设为2则可以产生logging.log4j.1,logging.log4j.2两个滚动文件和一个logging.log4j文件。

 

举例说明 特定包的级别和目的地

先增加一个包,新建一个类:

Java 代码

  1. package com.coderdream.log4jDao;    
  2. import org.apache.log4j.Logger;    
  3. public class HelloDao {  
  4.     private static Logger logger = Logger.getLogger(HelloDao.class);  
  5.     public static void main(String[] args) {  
  6.         //  记录 debug 级别的信息   
  7.         logger.debug("This is debug message from Dao.");  
  8.         //  记录 info 级别的信息   
  9.         logger.info("This is info message from Dao.");  
  10.         //  记录 error 级别的信息   
  11.         logger.error("This is error message from Dao.");  
  12.     }  
  13. }  

      如果这个类作为基类,如 J2EE 中的 BaseDao  BaseAction  BaseService 等等,则我们可以将各层的日志信息分类输出到各个文件。

Properties 代码

  1. # 省略根,只设置特定包的级别和目的地    
  2. log4j.logger.com.coderdream.log4j =debug,appender1  
  3. log4j.logger.com.coderdream.log4jDao =info,appender1,appender2    
  4. # 输出到控制台   
  5. log4j.appender.appender1=org.apache.log4j.ConsoleAppender  
  6. log4j.appender.appender1.layout=org.apache.log4j.PatternLayout  
  7. log4j.appender.appender1.layout.ConversionPattern =[%d{yy/MM/dd HH:mm:ss:SSS}][%C-%M] %m%n  
  8. # 输出到文件 ( 这里默认为追加方式 )  
  9. log4j.appender.appender2=org.apache.log4j.FileAppender  
  10. log4j.appender.appender2.File=c:/Log4JDemo07_Dao.log  
  11.  log4j.appender.appender2.layout=org.apache.log4j.PatternLayout  
  12. log4j.appender.appender2.layout.ConversionPattern=[%d{HH:mm:ss:SSS}][%C-%M] -%m%n   

###. 指定 com.neusoft 包下的所有类的等级为 DEBUG 。可以把 com.neusoft 改为自己项目所用的包名。 
log4j.logger.com.neusoft=DEBUG 

###. 如果项目中没有配置 EHCache ,则配置以下两句为 ERROR 。 
log4j.logger.com.opensymphony.oscache=ERROR 
log4j.logger.net.sf.navigator=ERROR 

### struts 配置 
log4j.logger.org.apache.commons=ERROR 

log4j.logger.org.apache.struts=WARN 

### displaytag 配置 
log4j.logger.org.displaytag=ERROR 

### .spring 配置 
log4j.logger.org.springframework=DEBUG 

### . ibatis 配置 
log4j.logger.com.ibatis.db=WARN 
### . hibernate 配置 

Log4j 输出格式转换字符说明 
================= 
c  category的名称,可使用{n}限制输出的精度。例如:logger名为"a.b.c",%c{2}将输出"b.c"。 

C  产生log事件的java完全限定类名。可使用{n}限制输出的精度。例如:“org.apache.xyz.SomeClass”,%C{2}将输出“SomeClass”。 

d  时间和日期的输出格式,例如:%d{yyyy MM dd HH:mm:ss,SS},可不带后面的日期格式字符。 

F  产生log事件的java源文件名,带“.java”后缀及包名称。 

l  log发生位置的详细描述,包括方法名、文件名及行号。 

L  log发生在源文件中的位置。 

m  log事件的消息内容。 

M  log发生时所在的方法名称。 

n  根据所运行的平台输出相应的行分隔字符。 

p  log事件的级别。 

r  自程序运行至log事件产生所经过的时间。 

t  产生log的线程名称。 

你的问题可以有下面的格式串输出: 
%d %-5p %l - %m%n 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326604190&siteId=291194637