slf4j+log4j configuration

The difference between SLF4J and Log4j:

  • SLF4J, the Simple Logging Facade for Java, is not a specific logging solution, it only serves a variety of logging systems. Officially, SLF4J is a simple facade for logging systems, allowing end users to deploy their applications with the logging system they want.
  • Log4j is an actual logging solution. By using Log4j, you can control the destination of log information delivery to consoles, files, GUI components, and even socket servers, NT event loggers, UNIX Syslog daemons, etc.; you can also Control the output format of each log; by defining the level of each log information, the log generation process can be controlled in more detail.

Since slf4j is a log output scheme that the user chooses according to the actual situation during deployment (for example, choose log4j or choose the log system that comes with JDK), the combination of jar packages will definitely be involved. As shown below:

It can be seen from the figure that:

If slf4j+log4j is used, the jar packages involved are as follows: slf4j-api-1.7.5.jar, log4j-1.2.16.jar, slf4j-log4j12-1.7.10.jar

If you use the logging system that comes with slf4j+JDK, the jar packages involved are as follows: slf4j-api-1.5.10.jar and slf4j-jdk14-1.5.10.jar

Maven dependencies are as follows:

 

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.10</version>
</dependency>

 

In the project, the framework will obtain the specific log system used. If slf4j exists in the project, the jar package will be used first, and then the specific logging system will be selected through slf4j. However, it should be noted that when the intermediate jar package (slf4j-log4j12.jar or slf4j-jdk14-1.5.10.jar) is not added, only the log will be missed, which may be related to slf4j choosing a default log system There are relationships.

 

Especially for a log system commonly used in projects like Log4j, it is necessary to pay special attention to the fact that if only Log4j is used in the project, the project cannot contain any jar package of slf4j, otherwise there will be a problem that unnecessary logs cannot be printed.

It can also be understood as: either add the full jar package, and do not omit the intermediate jar package. Either use single log4j.

 

The log printing layout and parameter settings are set according to the format of the specific log system, click Log4j configuration

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327083762&siteId=291194637