Log4j --- file parsing and syntax

Log4j ------ is Apache is an open source project, through the use of Log4j, we can control the destination log information is conveyed console , a file, the GUI components, or even socket servers, NT event logger, UNIX  Syslog daemon and so on.

    We can also control the output format of each log ; by the level of definition of each log message , Log4j can output not only content to the console, but also the contents of the output to a file, we can more carefully control the build process logs.

    These can be a profile to be configured flexibly, without the need to modify the application code. Log4j supports two configuration file formats: one is the XML format. One is the properties file properties.

Component -------- Lo4g the log level: off (the highest level)> fatal (fatal error)> error (error)> warn (warning)> info (general information)> debug (debug information) > All ( the lowest level)

    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.

  Note: Each log files are set up log level, the log file records can only be greater than or equal to the level of information, there are four commonly used.

### record log output level> Debug ###
log4j.appender.D.Threshold = DEBUG

  

------- Lo4g assembly in Log4j.Appender destination - log information output destination

      ConsoleAppender ------ (console),
      FileAppender ------------ (file)
      DailyRollingFileAppender ----- (produce a daily log file).
      ----- ollingFileAppender (file size reaches the specified size to generate a new file)
      WriterAppender --------- (log information to any designated place in stream format) 

(1) ConsoleAppender options:

    Threshold = WARN: log information specified minimum level of output, the default is DEBUG.
    ImmediateFlush = true: indicates that all messages will be immediately output, the output is not set to false, the default value is true.
    Target = System.err: The default is System.out.

(2) FileAppender options:

    Threshold = WARN: log information specified minimum level of output, the default is DEBUG.
    ImmediateFlush = true: indicates that all messages will be immediately output, the output is not set to false, the default value is true.
    Append = false: true indicates that the message designated to document, the contents of the message is covered with false documents specified, the default is true.
    File = D: /logs/logging.log4j: logging.log4j news output to a file.

(3) DailyRollingFileAppender options:

    Threshold = WARN: log information specified minimum level of output, the default is DEBUG.
    ImmediateFlush = true: indicates that all messages will be immediately output, the output is not set to false, the default value is true.
    Append = false: true indicates that the message designated to document, the contents of the message is covered with false documents specified, the default is true.
    File = D: /logs/logging.log4j: Specifies the current output to logging.log4j message file.
    DatePattern = yyyy-MM '.' : Once a month rolling log files, which produce a new log file each month. The current monthly log file named logging.log4j, the previous month's log file named logging.log4j.yyyy-MM.
    In addition, it can also be specified by week, day, hour, minute, to scroll the log file, corresponding to the following format:
    . 1) the MM-YYYY '.': Month
    '.' 2) yyyy-ww : week
    3) '. 'yyyy-MM-dd: day
    4)'. 'yyyy-MM -dd-a: twice a day
    5)'. 'yyyy-MM -dd-HH: hour
    . 6)' 'yyyy-MM -dd- HH-mm: min

(4) RollingFileAppender options:

    Threshold = WARN: log information specified minimum level of output, the default is DEBUG.
    ImmediateFlush = true: indicates that all messages will be immediately output, the output is not set to false, the default value is true.
    Append = false: true indicates that the message designated to document, the contents of the message is covered with false documents specified, the default is true.
    File = D: /logs/logging.log4j: logging.log4j news output to a file.
    MaxFileSize = 100KB: suffix can be KB, MB or GB. When the log file reaches this size, it will automatically scroll the contents of the upcoming move logging.log4j.1 original file.
    MaxBackupIndex = 2: Specifies the maximum number of files that can be produced rolling, for example, set two can produce logging.log4j.1, logging.log4j.2 two rolling papers and a logging.log4j file.   

### log information output destination - console - define the output terminal named Console ###
log4j.appender.Console = org.apache.log4j.ConsoleAppender
### log destination information --- every day a file - defined output terminal named D ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender

 

Component ------- format of log information (layout): .. Log4j.Appender named layout    

    org.apache.log4j.HTMLLayout (with HTML layout table form),
    org.apache.log4j.PatternLayout (the flexibility to specify the layout mode),
    Org.apache.log4j.SimpleLayout (containing level information and log information string ),
    org.apache.log4j.TTCCLayout (including the time the log generated, thread , type information, etc.)
(1) HTMLLayout options:
  LocationInfo = to true: java output file name and line number, the default value is false.
  Title = My Logging: The default value is Log4J Log Messages.
(2) PatternLayout options:
  ConversionPattern% =% n-m: set the display message in what format.
 
Using the specific format pattern layout should ConversionPattern, the print parameters specified print information are as follows:
      % M output code specified in the message;
      % M when the log output printing method name;
      % p output priority, i.e. the DEBUG, the INFO , WARN, ERROR, FATAL;
      % R output from the application starts to output the log information on the number of milliseconds spent;
      % C output category belongs, where the class is usually full name;
      % T thread name of the output generated log events;
      Output% n Enter a newline, Windows platform "rn", Unix platform "n";
      Date or time% d log output time points, the default format is ISO8601, you can specify the format followed in, for example:% d {yyyy-MM-dd HH: mm: ss, SSS}, similar output: 2002-10- 18 22: 10: 28,921;
      % L output log event occurrence position and the number of lines in the code;
### record log information format ### 
log4j.appender.D.layout = org.apache.log4j.PatternLayout

 Output format of the log information ###
 log4j.appender.D.layout.ConversionPattern =% -d {yyyy-MM -dd HH: mm: ss} [% t:% r] - [% p]% m % n

  

    ----- log4j.properties first line of code in the configuration file as follows:    

log4j.rootLogger = debug,Console,D,E

     Parsing: rootLogger expressed as the root logger, only one. By invoking the Logger.getRootLogger () function, generally have a test class for the object parameters, to obtain root logger object.

        represents a non-root loggerName recorder, the recorder can not have multiple roots. By Logger.getLogger () function, to obtain non-root object record

     Here the root of the first parameter recorder output priority, as defined by the level here, the application can be controlled to switch the appropriate level of log information, only equal to and higher than before this level for processing logs.

 

### is defined as a configuration log root Logger-- priority --- output terminal ###
### defined by a level where you can control the application of the log information corresponding to the switching level, equal to and only above this level and then processed ###
log4j.rootLogger = debug,Console,D

### outputs the control information to the lift ### 
log4j.appender.Console = org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target = System.out
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

Output ### to E: // logs / log.log ###   

output log information set ### D named - file output - generates a ### day
log4j.appender.D = org.apache.log4j.FileAppender
Set the log file ### production sites ###
log4j.appender.D.File = E: / logs // log.log 
### whether additional information is   false - Clear ###
log4j.appender.D.Append = to true 
### logging output level > the Debug ###
log4j.appender.D.Threshold = DEBUG
Expression logging output ### ### 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
The output format of the log information ### ###
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

// test file

package cn.log4j.test;

import org.apache.log4j.Logger;
import org.apache.log4j.chainsaw.Main;

public class Test {
static Logger logger = Logger.getLogger(Test.class);
public static void main(String[] args) {
logger.debug("测试");
logger.warn("警告");
}
}

 

Guess you like

Origin www.cnblogs.com/Tanggula-pioneer/p/11797565.html