log4j (a)

First, the basics

  Log4j has three important components: the output format grade level- log information, log information output destination appenders-, layouts- log information.

  Level level- log information : level priority log is divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL or custom levels. 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, only equal to and above this level and then processed, the application of all the DEBUG level log information will not be printed. ALL: Print all logs, OFF: close all log output.

  Appenders- log information output destination: Specify the log will be printed to the console or file.

  • ConsoleAppender (console)
  • FileAppender (file)
  • DailyRollingFileAppender (produce a daily log file)
  • The RollingFileAppender (create a new file when the file size reaches the specified size)
  • WriteAppender (log stream format information is sent to any designated place)
  • JDBCAppender (save the log information to the database)

  Layouts- log information output format: controls the display contents of the log information.

  • Full name% c output log information belongs to the class
  • Date or time% d log output time points, such as:% d {yyyy-MM-dd HH: mm: ss}, similar output: 2019-05-29 09:20:21
  • The class name of the class% f output log information belongs
  • Occurrence location% l output log events, namely the output log information statement in its first few lines of their class
  • % M output information specified in the code, such as the log (error) error
  • % N output a carriage return, particularly a carriage return with the current operating system
  • % P output priority, i.e. DEBUG, INFO, WARN, ERROR, FATAL
  • % R output from the application starts to output the log information is the number of milliseconds spent
  • % T output to produce the event log name of thread
  • % X output current thread of NDC (nested diagnostic context)
  • File name where output is generated log messages% F
  • %% outputs a "%" character

  log4j configuration supports two file formats, one is the XML ( Standard Generalized Markup Language application under) format, one is the log4j.properties Java properties file (key = value).

     log4j.properties file as a configuration file Description:

 1 log4j.rootLogger = debug,Console,D,E
 2 
 3 log4j.appender.Console = org.apache.log4j.ConsoleAppender
 4 log4j.appender.Console.Target = System.out
 5 log4j.appender.Console.layout = org.apache.log4j.PatternLayout
 6 log4j.appender.Console.layout.ConversionPattern = [%p] %d{yyyy-MM-dd HH:mm:ss,SSS} [ %t:%l] - %m%n
 7 
 8 log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
 9 log4j.appender.D.File = E://logs/log.log
10 log4j.appender.D.Append = true
11 log4j.appender.D.Threshold = DEBUG
12 log4j.appender.D.layout = org.apache.log4j.PatternLayout
13 log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
14 
15 log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
16 log4j.appender.E.File =E://logs/error.log
17 log4j.appender.E.Append = true
18 log4j.appender.E.Threshold = ERROR
19 log4j.appender.E.layout = org.apache.log4j.PatternLayout
20 log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
   ①, configure the root Logger
  Logger handles most operations logging.
  The syntax is:
  log4j.rootLogger = [ level ] , appenderName, appenderName, …
  is the priority level logging, front described.
 
   ②, configuration information log output destination Appender
  Appender responsible for controlling the output of the logging operation.
  The syntax is:
log4j.appender.appenderName = fully.qualified.name.of.appender.class
log4j.appender.appenderName.option1 = value1
…
log4j.appender.appenderName.optionN = valueN
  appenderName here is, can be arbitrarily defined in ① named.
  Which, appender Log4j provided are the following:
  org.apache.log4j.ConsoleAppender (console),
  org.apache.log4j.FileAppender (file),
  org.apache.log4j.DailyRollingFileAppender (produce a daily log file),
  org.apache.log4j.RollingFileAppender (file size reaches the specified size to generate a new file), by setting the file size log4j.appender.R.MaxFileSize = 100KB, may also be provided by log4j.appender.R.MaxBackupIndex = 1 to save a backup file .
  Org.apache.log4j.WriterAppender (log stream format information is sent to any designated place)
  例如:log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  Define a named destination output to stdout, ConsoleAppender for the console.
 
   Format (layout) Layout ③, log information
  Layout is responsible for formatting the output of Appender.
  The syntax is:
log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class
log4j.appender.appenderName.layout.option1 = value1
…
log4j.appender.appenderName.layout.optionN = valueN
  Which, layout Log4j provided are the following:
  org.apache.log4j.HTMLLayout (in HTML table format layout),
  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.)
 
Second, configure log4j
   1, introducing dependencies of the package log4j
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
</dependency>
        

 

  2, define the format and content of the log. (Supports two methods: xml or properties)

  xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
    <appender name="Console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%p] %d{yyyy-MM-dd HH:mm:ss,SSS} [ %t:%l] - %m%n"/>
        </layout>
    </appender>
    <appender name="D" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="file" value="E://logs/log.log"/>
        <param name="append" value="false"/>
        <param name="threshold" value="debug"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n"/>
        </layout>
    </appender>
    <appender name="E" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="file" value="E://logs/error.log"/>
        <param name="append" value="false"/>
        <param name="threshold" value="error"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%%-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n"/>
        </layout>
    </appender>
    <logger name="e" additivity="true">
        <level value="error"/>
        <appender-ref ref="E"/>
    </logger>
    <logger name="d" additivity="true">
        <level value="debug"/>
        <appender-ref ref="D"/>
    </logger>
    <root>
        <level value="debug"/>
        <appender-ref ref="Console"/>
        <appender-ref ref="D"/>
        <appender-ref ref="E"/>
    </root>
</log4j:configuration>

 

  properties:

 1 log4j.rootLogger = debug,Console,D,E
 2 
 3 log4j.appender.Console = org.apache.log4j.ConsoleAppender
 4 log4j.appender.Console.Target = System.out
 5 log4j.appender.Console.layout = org.apache.log4j.PatternLayout
 6 log4j.appender.Console.layout.ConversionPattern = [%p] %d{yyyy-MM-dd HH:mm:ss,SSS} [ %t:%l] - %m%n
 7 
 8 log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
 9 log4j.appender.D.File = E://logs/log.log
10 log4j.appender.D.Append = true
11 log4j.appender.D.Threshold = DEBUG
12 log4j.appender.D.layout = org.apache.log4j.PatternLayout
13 log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
14 
15 log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
16 log4j.appender.E.File =E://logs/error.log
17 log4j.appender.E.Append = true
18 log4j.appender.E.Threshold = ERROR
19 log4j.appender.E.layout = org.apache.log4j.PatternLayout
20 log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

  

  3, initialize log4j (without careful analysis, follow-up with a deeper understanding supplemented on)

  There are three ways to initialize log4j:
  • BasicConfigurator.configure (): This method is simple log4j initialization, to add a ConsoleAppender root logger, while PatternLayout set to "% -4r [% t]% -5p% c% x -% m% n", log level to debug.

  • PropertyConfigurator.configure (String configFilename): reads the configuration file using the keys to write.

  • DOMConfigurator.configure (String filename): read XML configuration file the form.

  

  4, uses log4j

  Each class will need to log on through Logger.getLogger method and pass a string (usually full class name) Gets a Logger object. The Logger object by being defined as static final.

  If there is a class X, the following three expressions are possible in the same package com.xxx:

  Logger.getLogger(“com.xxx.X”);

  Logger.getLogger(X.class.getName());

  Logger.getLogger(X.class);

 

Reference: https://juejin.im/entry/585d1f36da2f6000658485ee

   https://blog.csdn.net/u013870094/article/details/79518028

   https://baike.baidu.com/item/log4j/480673?fr=aladdin#2

Guess you like

Origin www.cnblogs.com/Jin1000x/p/10938958.html