Log4j and log4j2

Keywords: log4j Attribute Apache xml

I believe that many programmer ape friends are familiar with log4j, log4j can be said to be accompanied by the vast majority of friends to open the programming. I don't know what log4j used to be, at least in my career, it was the era of log4j that led me to start.

Log4j is an open source project of Apache. We don't go into its origin, but as far as I know, log4j 1 is no longer updated.  
The following quotes the original text of the official website:

End of Life On August 5, 2015 the Logging Services Project Management 
Committee announced that Log4j 1.x had reached end of life. For 
complete text of the announcement please see the Apache Blog. Users of 
Log4j 1 are recommended to upgrade to Apache Log4j 2. 
Yes, log4j stopped at version 1.x and log4j 2, which is what we're going to talk about today.

Looking back at log4j, I remember how many nights I struggled with to configure the ibatis console to print logs when mybatis was still called ibatis, and how happy I felt when I finally configured it. Not much nonsense. Now I will talk about the differences between log4j and log4j 2 in the way of enumeration.

1. Configuration file type
Log4j is the main configuration file through A. properties file, but now log4j 2 has abandoned this way, using. xml,. json or. jsn. This may also be a necessity of technological development, after all, the readability of properties files is really a little poor.

2. Core JAR packages
log4j only needs to introduce a jar package.

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

log4j 2 requires two cores

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.5</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.5</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Did you find that the package paths of log4j and log4j 2 are different? Apache updates the package paths in order to distinguish, so that you can even use two versions of log output in a project! (But I don't think you're that naughty, hey hey)

3. Document Rendering
For log4j to take effect, we need to configure it in web.xml.

 <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener> <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:config/log4j.properties</param-value>
  </context-param>
  <context-param>
    <param-name>log4jRefreshInterval</param-name>
    <param-value>60000</param-value>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

The purpose of this configuration is to tell the project where to load the log4j configuration file and define a scanner so that the log4j configuration file can be placed at will.  
Log4j2 is relatively simple. Take the maven project as an example, we just need to put log4j2.xml in the project resource directory. Let's remember a detail, log4j2.xml, not log4j.xml, XML name less than 2 can not!!

  • Like to be a learning partner, interested in studying, according to my estimation should be in the log4j2 package configuration dead, and you can try how to customize the location of log4j2.xml.

4.Log calls
Both log4j and log4j2 calls are simple.  
log4j:

import org.apache.log4j.Logger;
private final Logger LOGGER = Logger.getLogger(Test.class.getName());
  • 1
  • 2
  • 1
  • 2

log4j2:

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
private static Logger logger = LogManager.getLogger(Test.class.getName());
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

5. Configuration file format
The most important difference is the difference of configuration files. When you use them, you can configure them according to your situation.  
log4j2 cases were as follows:

<?xml version="1.0" encoding="UTF-8"?>    
<configuration status="error">  
<!--     Define all appender -->  
    <appenders>  
<!--         Configuration of this output console -->  
        <Console name="Console" target="SYSTEM_OUT">  
<!--             Console output only level Information at and above levels( onMatch),Other direct refusals( onMismatch) -->  
            <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>  
<!--             This is known as the format of the output log. -->  
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>  
        </Console>  

<!--         The file prints out all the information. This log Every time the program is run, it will be cleared automatically. append Attribute determination, which is also useful for temporary testing -->  
<!--         append by TRUE Represents that the message is added to the specified file. false Represents that the message overrides the specified file content, with the default value of __________ true -->  
        <File name="log" fileName="log/test.log" append="false">  
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>  
        </File>  

<!--          Adding filters ThresholdFilter,Categories above a certain level can be selectively output  onMatch="ACCEPT" onMismatch="DENY"It means to match and accept.,Otherwise refuse directly  -->  
        <File name="ERROR" fileName="logs/error.log">  
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>  
            <PatternLayout pattern="%d{yyyy.MM.dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>  
        </File>  

<!--         This prints out all the information, each time the size exceeds size,Then this size Size logs are automatically saved by year-Folder created in January and compressed for archiving -->  
        <RollingFile name="RollingFile" fileName="logs/web.log"  
                     filePattern="logs/$${date:yyyy-MM}/web-%d{MM-dd-yyyy}-%i.log.gz">  
            <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>  
            <SizeBasedTriggeringPolicy size="2MB"/>  
        </RollingFile>  
    </appenders>  

<!--     Then define logger,Only defined logger And introduced appender,appender It will take effect. -->  
    <loggers>  
<!--         Create a default root Of logger -->  
        <root level="trace">  
            <appender-ref ref="RollingFile"/>  
            <appender-ref ref="Console"/>  
            <appender-ref ref="ERROR" />  
            <appender-ref ref="log"/>  
        </root>  

    </loggers>  
</configuration>  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

Conclusion:
Technology is always progressing. We can't confine ourselves to the known field of knowledge. We should constantly innovate and try!

Attachment:

Level of logs: \\\\\\\
Now we need to call logger's method, but there are many methods in this logger object, so we need to know log4j's log level first. log4j specifies the default levels: trace < debug < info < warn < error < fatal, etc. Here's an explanation:

  1. The relationship between levels is inclusion, which means that if you set the log level to trace, all logs larger than or equal to this level will be output.
  2. Basically, the default level doesn't make much difference. It's a default setting. You can define your own level through its API. You can also call these methods at will, but you have to deal with them well in the configuration file, otherwise it will not play the role of the log, and it is not easy to read, equivalent to a specification, you need to fully define a set of can, need not be very necessary. From the results of our experiments, we can see that the default priority of log4j is ERROR or WARN (actually ERROR).
  3. The meaning of these different levels is easy to understand. Here is a brief introduction.

    • trace:  
      It's tracing, that is, you can write a trace output when the program advances below, so traces should be very much, but that's OK, we can set the lowest log level to keep him from output.

    Debug: debug? I usually use this as the lowest level, trace is not used at all. Is it better to use the debug function of eclipse or idea when you can't.

    • info: Output information that you are interested in or important, which is the most useful.

    • warn: Some messages are not error messages, but there are also hints to programmers that code validation in eclipse is not error.
      And warn (not a mistake, but note, for example, the following depressed method).

    • error: error message. It's also used a lot.

    • fatal: The level is higher. Major errors, this level you can stop the program directly, should not be the error! Don't be so nervous. It's a matter of degree.

Posted by danjar on Fri, 22 Mar 2019 20:00:53 -0700