Apache open source logging framework Log4j Configuration Guide

Apache Log4j is an open source project, through the use of Log4j, we can control the destination log information delivery is the console, files, GUI components, or even socket servers, NT event logger, UNIX Syslog daemon etc; we also you can control the output format of each log; each defined by a level of log information, we were able to more carefully control the log generation process. The most convenient is that these can be flexibly configured via a configuration file, without modifying the application code. This guide describes Log4j 1.x version of the configuration and use of Log4j Download the latest version: http: //www.apache.org/dyn/closer.cgi/logging/log4j/1.2.17/log4j-1.2.17. zip

A disposed in Eclipse Log4j

1.1, the new Java project import the package log4j-1.2.17.jar, project below:

 

 

 

1.2, create log4j.propertiese profile in src, copy and paste the following into the file:

### logger主要定义log4j支持的日志级别及输出目的地 ###
log4j.rootLogger=debug,stdout,file
 
### 配置控制台输出及输出格式 ###
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=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
 
### 输出DEBUG级别以上的日志到工程根目录下的logs/log.log文件,可以自行修改为绝对路径 ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=logs/log.log
log4j.appender.file.Append=true
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ]  %m%n

Note: Paste into Eclipse, the partners will show some small "garbled": \ u6839, which is Unicode encoding does not affect the execution of the program.

1.3, write code in Java classes

package com.xuetang9.kenny;
import org.apache.log4j.Logger;
/**
 * Use Log4j print log information <br/>
 * @author Kenny
 * @version 1.0
 * 9:41:13 @date 2018 Nian 1 afternoon of May 8
 * @Copyright old nine school
 */
public  class  LoggerTest  {
    private static Logger logger = Logger.getLogger(LoggerTest.class);
    public  static  void  main ( String[]  args)  {
        // logging behavior is hierarchical, divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL or level that you define.
        // Log4j recommended to use only four levels, from highest to lowest priority are ERROR, WARN, INFO, DEBUG.
        logger.debug ( "record-level debug information");
        logger.info ( "info record level information");
        logger.warn ( "Record warn level information");
    }
}

1.4, output:

Console:

 

 


logs / log.log file:

 

 

 

Two, Log4j basic use

Log4j is highly configurable, and through external configuration files at run time. It is based on the priority level records, and provides a mechanism to indicate the record information to a number of destinations, such as: database, file, console, UNIX system logs.

Log4j has three main components:

- loggers: responsible for capturing log information.

- appenders: responsible for publishing the log information to a different destination of choice.

- layouts: responsible for log information formatted in different styles.

Definition 2.1, the configuration file

log4j can also be set through the configuration file, currently supports two configuration file format: xml files and properties files (recommended).

1, configure the root Logger, the syntax is:

log4j.rootLogger=[level], appenderName1, appenderName2, ....

represents logging level priority into OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL or custom level (E, F, G, etc.). Log4j recommended to use only four levels, from highest to lowest priority are ERROR, WARN, INFO, DEBUG. Defined by the level here, we can switch to control the application of the appropriate level of the log information. For example, in the definition of the INFO level here, the application of all the DEBUG level log information will not be printed. appenderName refers to output log information to which place. You can specify multiple output destinations.

2, configuration information log output destination (the Appender), syntax is as follows:

log4j.appender.appenderName=fully.qualified.name.of.appender.class
log4j.appender.appenderName.option1=value1
    ...............
log4j.appender.appenderName.option=valueN

Which, appender log4j provided are the following, according to project needs to use:

org.apache.log4j.ConsoleAppender            输出到控制台
org.apache.log4j.FileAppender               输出到文件
org.apache.log4j.DailyRollingFileAppender   每天产生一个日志文件
org.apache.log4j.RollingFileAppender        文件大小到达指定尺寸的时候产生一个新的文件
org.apache.log4j.WriterAppender             将日志信息以流格式发送到任意指定的地方

3, the configuration log output format (layout), the following syntax:

log4j.appender.appenderName.layout=fully.qualified.name.of.layout.class
log4j.appender.appenderName.layout.option1=value1
    ..............
log4j.appender.appenderName.layout.option=valueN

Format layout Log4j provided are the following:

org.apache.log4j.HTMLLayout     HTML表格形式布局
org.apache.log4j.PatternLayout  可以灵活地指定布局模式(常用)
org.apache.log4j.SimpleLayout   包含日志信息的级别和信息字符串
org.apache.log4j.TTCCLayout     包含日志产生的时间、线程、类别等等信息

Log4j similar C language printf function to format the log information, print parameters are as follows:

%p 输出优先级,即DEBUGINFOWARNERRORFATAL
%r 输出自应用启动到输出该log信息耗费的毫秒数
%c 输出所属的类目,通常就是所在类的全名
%t 输出产生该日志事件的线程名
%n 输出一个回车换行符,Windows平台为“rn”Unix平台为“n”
%d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy MMM dd HH:mm:ss,SSS},输出类似:20181231 235959123
%l 输出日志事件的发生位置,包括类名、发生的线程,以及在代码中的行数。如:LoggerTest.main(LoggerTest.java:18)

4, the log level

Each Logger are a logging level (log level), for controlling the output log information. Log level from high to low is divided into:

A: off the highest level for close all logging.

B: fatal pointed out that every serious error occurs if exiting the application.

C: error error pointed out that although the incident, but still does not affect the continued operation of the system.

D: warm indicate that there will be a potential error conditions.

E: info general and in coarse-grained level, emphasizing the run the whole application.

F: debug generally used for fine-grained level, for debugging applications are very helpful.

G: all the lowest level, to open all logging.

These levels are defined above in org.apache.log4j.Level class. Log4j recommended to use only four levels, from high to low priority is error, warn, info, and debug. By using log level, you may control the output level corresponding to the log information of the application. For example, if the info level b, then all levels below the log information info application program (such as debug) will not be printed.

Dry notes more attention to micro-channel public number: the old nine school

Guess you like

Origin www.cnblogs.com/ljxt/p/11613038.html