Log4j's instructions

Basic use Log4j

Log4j consists of three major components: the priority, the log information output destination, the output format of the log information to the log information. Log information of highest to lowest priority has ERROR, WARN, INFO, DEBUG, respectively, is used to specify the degree of importance of this log information; log information output destination specified log will be printed to the console or file; and outputting format the control displays the contents of the log information.

2.1, custom profiles

In fact, you can also do not use the configuration file, but configure Log4j environment in code. However, using the configuration file will make your applications more flexible. Log4j supports two configuration file format An XML format file, one is Java properties file (key = value). Here we introduce the use of Java properties file as a configuration file method:
1. Configure the root Logger, the syntax is:

log4j.rootLogger = [ level ] , appenderName, appenderName, …

One, level logging is a priority, divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL or you define level. Log4j recommended to use only four levels, from highest to lowest priority are ERROR, WARN, INFO, DEBUG. Defined by the level here, you can control the switch to log the application of the appropriate level of 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 B refers to output log information to which place. You can specify multiple output destinations.

2. Configure the log information output destination Appender, the syntax is:

  1.  
    log4j.appender.appenderName = fully.qualified.name.of.appender.class
  2.  
    log4j.appender.appenderName.option1 = value1
  3.  
  4.  
    log4j.appender.appenderName.option = valueN

Which, appender Log4j provided are the following:

  1.  
    ORG .apache .log4j .ConsoleAppender (console),
  2.  
    ORG .apache .log4j .FileAppender (file),
  3.  
    ORG .apache .log4j .DailyRollingFileAppender (produce a daily log file),
  4.  
    ORG .apache .log4j .RollingFileAppender (file size reaches the specified size when generating a new file),
  5.  
    ORG .apache .log4j .WriterAppender (log stream format information is sent to any designated place)

3. Configure the log information format (layout), its syntax is:

  1.  
    log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class
  2.  
    log4j.appender.appenderName.layout.option1 = value1
  3.  
  4.  
    log4j.appender.appenderName.layout.option = valueN

Wherein, layout Log4j are provided to several e:

  1.  
    ORG .apache .log4j .HTMLLayout (with layout HTML table form),
  2.  
    ORG .apache .log4j .PatternLayout (the flexibility to specify the layout mode),
  3.  
    ORG .apache .log4j .SimpleLayout (containing level information and log information string),
  4.  
    ORG .apache .log4j .TTCCLayout (including the time the log generated, thread, type information, etc.)

Log4J similar to the C language printf function log information formatted for printing, the printing parameters are as follows:% m output code specified in the message

% p output priority, i.e. the DEBUG, the INFO, WARN, ERROR, FATAL   
% R & lt output from the application starts to output the number of milliseconds spent log information   
% c output category belongs, usually the full name of the class where   
% t produce output the thread name of the log events   
output% n Enter a newline, Windows platform "rn", Unix platform "n"   
date or time% d output log point in time, the default format is ISO8601, you can specify later format, for example:% d {yyy MMM dd HH : mm: ss, SSS}, similar output: 2002 10 dated 18 is Day 22 is: 10: 28, the occurrence location 921% l output log events, including category names, occurs thread, as well as the number of lines in the code. Example: Testlog4.main (TestLog4.java: 10)

2.2, using code Log4j

1. Recorder obtained

Use Log4j, the first step is to get the logger, the logger will be responsible for control log information. The syntax is:

public static Logger getLogger( String name)

Obtaining records by specifying the name, if necessary, the name was to create a new recorder. Name generally take the name of the class, such as:

static Logger logger = Logger.getLogger ( ServerWithLog4j.class.getName () )

2. Read the configuration file

After obtaining a logger, the second step will configure Log4j environment, the syntax is:

  1.  
    BasicConfigurator .configure (): Automatic use the default Log4j environment.
  2.  
    PropertyConfigurator .configure ( String configFilename): read using a Java properties file written profile.
  3.  
    DOMConfigurator .configure ( String filename): read the XML format configuration files.

3. Insert recording information (log information format)

When the two necessary steps are finished, you can easily use the logging statements with different priorities into any place you want to log in, its syntax is as follows:

  1.  
    Logger.debug ( Object message ) ;
  2.  
    Logger.info ( Object message ) ;
  3.  
    Logger.warn ( Object message ) ;
  4.  
    Logger.error ( Object message ) ;

2.3, 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 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.

Log4j configuration file log4j.properties

### to set the output level, debug output will be more than ### here 
### Log4j log information according to the degree of importance, sub-OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL 
### Log4j official suggested the actual practical then, Log4j recommended to use only four levels, highest to lowest priority are ERROR, WARN, the iNFO, the DEBUG 

Log4j.rootLogger = Debug, stdout, D, E 

### outputs the control information to the lift ### 
log4j.appender = org.apache.log4j.ConsoleAppender tells that .stdout 
log4j.appender.stdout.Target = the 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 

above ### to output the log file level DEBUG E: //JavaProjects/Log4j/debug.log # ## 
log4j.appender.D = org.apache.log4j.FileAppender 
log4j.appender.D.File = E: //JavaProjects/Log4j/debug.log 
log4j.appender.D.Append to true = 
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 输出ERROR 级别以上的日志到文件E://JavaProjects/Log4j/error.log ###
log4j.appender.E = org.apache.log4j.FileAppender
log4j.appender.E.File = E://JavaProjects/Log4j/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

 

Guess you like

Origin www.cnblogs.com/schoolbag/p/10978801.html