Maven Configuration for slf4j and log4j

Keywords: log4j Apache Java xml

I. concept

The full name of SLF4J is Simple Logging Facade for Java, which is a simple log facade. SLF4J is not a specific logging framework, but serves as a simple facade for various logging frameworks, such as java.util.logging, logback and log4j.

SLF4J provides a unified logging interface, which abstracts the specific implementation of different logging systems. As long as it records according to the method provided by SLF4J, the final log format, recording level and output mode can be achieved by binding specific logging system.

The advantage of using SLF4J is that you only need to write log code in a uniform way, such as:

public class LoggerTest {

    private static final Logger logger = LoggerFactory.getLogger(Tester.class);

    public static void main(String[] args) {
        logger.info("Current Time: {}", System.currentTimeMillis());
    }
}

SLF4J supports {} as a placeholder, which is equivalent to% s in C language, without the need for string splicing. The efficiency has been significantly improved (see the results of the following run).

There is no need to care which log system is used to output the log in what style. Because they depend on the logging system that is bound to deploy the project.
For example, if SLF4J is used to record logs in a project and log4j is bound, the logs will be output in the style of log4j; in the later stage, it is necessary to output logs in the style of logback instead of changing the code in the project.

Two, steps

1. Add dependency to Maven's porn.xml file as follows (you can also manually introduce the following three packages)

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

After that, three packages will be added: slf4j-api-1.6.1.jar, slf4j-log4j12-1.7.2.jar, log4j-1.2.17.jar.

2. Then add log4j.properties under the project

 #config root logger  
 log4j.rootLogger = INFO,system.out  
 log4j.appender.system.out=org.apache.log4j.ConsoleAppender  
 log4j.appender.system.out.layout=org.apache.log4j.PatternLayout  
 log4j.appender.system.out.layout.ConversionPattern=MINAServer Logger-->%5p{%F:%L}-%m%n  
   
 #config this Project.file logger  
 log4j.logger.thisProject.file=INFO,thisProject.file.out  
 log4j.appender.thisProject.file.out=org.apache.log4j.DailyRollingFileAppender  
 log4j.appender.thisProject.file.out.File=logContentFile.log  
 log4j.appender.thisProject.file.out.layout=org.apache.log4j.PatternLayout

 

3. Add to the code

private static final Logger logger = LoggerFactory.getLogger(MINAServer.class);

 

If you want to output to the file system

1 log4j.rootLogger=INFO,R,stdout    
 2     
 3 log4j.appender.stdout=org.apache.log4j.ConsoleAppender    
 4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
 5 log4j.appender.stdout.layout.ConversionPattern=Logger-->%5p{%F:%L}-%m%n   
 6     
 7 log4j.appender.R=org.apache.log4j.DailyRollingFileAppender    
 8 log4j.appender.R.File=../logs/log  
 9 log4j.appender.R.layout=org.apache.log4j.PatternLayout  
10 log4j.appender.R.layout.ConversionPattern=Logger-->%5p{%F:%L}-%m%n

A log configuration method is generated every day:

log4j.rootLogger=DEBUG,DAILY_FILE
log4j.appender.DAILY_FILE=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.DAILY_FILE.file=${user.home}/logs/logging.log4j
log4j.appender.DAILY_FILE.DatePattern='_'yyyy-MM-dd'.log' 
log4j.appender.DAILY_FILE.ImmediateFlush=true
log4j.appender.DAILY_FILE.Append=true
log4j.appender.DAILY_FILE.layout=org.apache.log4j.PatternLayout 
log4j.appender.DAILY_FILE.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n

3. Replacement of Logging System

Looking at this, you may wonder: Since log4j is used, why use SLF4J to write log code? Is it not a waste of effort?

The answer is No. Assuming that we no longer need log4j, but instead want to log with java, all we need to do is to change the dependency slf4j-log4j12 of pom.xml to slf4j-jdk14 without any modifications to the above test code.

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-jdk14</artifactId>
  <version>1.8.0-alpha2</version>
</dependency>

Yes, it's that simple. Run the test code again:

June 16, 2017 10:32:30 p.m. com.jiapengcs.demos.slf4j.LoggerTest main
 Information: Current Time: 1497623550843
 June 16, 2017 10:32:30 p.m. com.jiapengcs.demos.slf4j.LoggerTest main
 Information: Current Time: 1497623550874
 June 16, 2017 10:32:30 p.m. com.jiapengcs.demos.slf4j.LoggerTest main
 Information: Current Time: 1497623550875
 June 16, 2017 10:32:30 p.m. com.jiapengcs.demos.slf4j.LoggerTest main
 Warning: warn log
 June 16, 2017 10:32:30 p.m. com.jiapengcs.demos.slf4j.LoggerTest main
 Information: info log
 June 16, 2017 10:32:30 p.m. com.jiapengcs.demos.slf4j.LoggerTest main
 Serious: error log

We found that at this point the log has become logging output.

Four, summary

The use of SLF4J is very simple, even on the official website, because it is too light, document space is not long.

In the Alibaba Java Development Manual (Official Edition), the first article of the log protocol mandates the use of SLF4J.

So start using SLF4J now!

Posted by jdlev on Tue, 18 Dec 2018 10:12:03 -0800