当前位置:网站首页>Alibaba development manual forces slf4j as the secret of facing. I have made it clear

Alibaba development manual forces slf4j as the secret of facing. I have made it clear

2020-11-10 12:02:47 Irving the procedural ape

It's been detailed before 、 A comprehensive introduction to Log4j, I believe that my friends have mastered . I'm reading the Songshan version of Alibaba development manual ( No, my little friend , Remember to ask me for ) When , I found one 「 mandatory 」 Log specification of nature :

Log system can not be used directly in application (Log4j、Logback) Medium API, Instead, use the API, for instance SLF4J, Log framework using facade mode , It is helpful to maintain and unify the log processing methods of various classes .

( Why did I knock down this piece of text , Because I found a language fault in Alibaba development manual , Look at the red part below )

( Maintain and unify , Putting unity on the last page is really awkward to read , And a little far fetched , Excuse me, the small edition of the manual is the language taught by the math teacher ?)

See this mandatory statute , I couldn't help asking :“ Why is Alibaba's development manual mandatory SLF4J As Log4J It's the face of ?” What is behind this “ not to be divulged ” The secret of ?

( Please match them by themselves CCTV 12 The kind of stage BGM)

PS: By the way, let's popularize a little knowledge for our friends , Alibaba development manual appears Jakarta It's actually Apache An open source project under the software foundation . Actually Commons It used to belong to Jakarta, Now it's as Apache The next individual project , The description in Alibaba's development manual is no longer appropriate , Change it to Apache Commons Logging It would be more appropriate .

( I can't help but pick a fault for Alibaba's development manual , Please forgive me “ be strict in one 's demands ” The attitude of doing things )

01、SLF4J What is it?

SLF4J yes Simple Logging Facade for Java Abbreviation (for≈4), It's a simple log facade , In appearance mode (Facade pattern, A design pattern , Provide a unified high-level interface for a group of interfaces in the subsystem , Make the subsystem easier to use ) Realization , Support java.util.logging、Log4J and Logback.

SLF4J The author is Log4J and Logback The author of , His GitHub The main page is as follows :

A cold feeling of autumn wind came to my face , Is there any ? Maybe the giant doesn't care to defend him GitHub Home page ? my GitHub The home page is miserable enough , I didn't expect that the giant was worse than me , Finally, I can boast that ,“ I , Silent king two ,GitHub Home page is more than SLF4J、Log4J and Logback The author of Ceki Gulcu It's much greener ......”

1996 Beginning of the year , The European secure electronic market project decided to write its own tracking API, In the end API It evolved into Log4j, It's been introduced and loved .

2002 year 2 month ,Sun Launched their own log pack java.util.logging( It can be called JUL), It is said that the realization idea has drawn lessons from Log4j, After all, at this time Log4j It's very mature .

2002 year 8 month ,Apache I launched my own log package , That's what the Alibaba development manual says JCL(Jakarta Commons Logging).JCL With great ambition , It's in JUL and Log4j Provides an interface of abstract layer based on , User friendly JUL and Log4j Switch between .

but JCL It doesn't seem to be very popular , Some people complain like this :

Ceki Gulcu I also feel that JCL Not good. , Or he wouldn't be in 2005 In 1949, I called myself SLF4J New projects , Right ? But there's always a price to pay ,SLF4J Only interfaces , It didn't come true , You can't force Java and Apache To achieve SLF4J Interface ? It's too hard , Not reality .

But the big guy is called the big guy , It's because he has something amazing beyond ordinary people , He was in SLF4J and JUL、Log4j、JCL Three bridges have been built between them :

Big guy started , Have ample food and clothing , Is there any ? Be cruel and connect your own Log4j Build a bridge .

Facing the domineering power of the giant , I just want to say something feebly ,“ SLF4J This facade is responsible for , You think it's easy to be ?”

02、SLF4J What pain points have been solved

In the spring and Autumn period and the Warring States period , Each country has its own currency , It's not appropriate to use another country's currency , Right ? That's more troublesome when there is trade , The currency is not unified , You can't trade directly , Because money may not be equivalent .

After Qin Shihuang unified the six kingdoms , We have launched a new monetary policy , One currency is used all over the country , Then the problem was solved .

You see , Same thing , The log system has JUL、JCL,Ceki Gulcu I wrote again 2 Kind of ,Log4j and Logback, Each has its own advantages and disadvantages , Plus tens of millions of users , Every man has his hobbyhorse , This leads to different applications may use different logging systems .

Suppose we're developing a system , Intended use SLF4J As a facade ,Log4j As a logging system , We used A frame , and A The facade of the frame is JCL, The log system is JUL, That's equivalent to maintaining two log systems , Right ?

That's tough !

Ceki Gulcu Thinking of this question , And helped us solve ! Look at SLF4J The solution given on the official website .

  • Use jcl-over-slf4j.jar Replace commons-logging.jar
  • introduce jul-to-slf4j.jar

To simulate this process , Let's build one that uses JCL Project .

First step , stay pom.

<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>

The second step , New test class :

package com.itwanger;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author  Wechat search 「 Silent king two 」, Reply key  PDF
 */
public class Demo {
    private static Log logger = LogFactory.getLog(Demo.class);
    public static void main(String[] args) {
        logger.info("jcl");
    }
}

This class will pass LogFactory Get one Log object , And use info() Method print a line of log .

In the process of debugging this code, you will find that ,Log There are four implementations of :

If there is no binding Log4j Words , Will default to choose Jdk14Logger—— It returns Logger object , It is java.util.logging.Logger, That is to say JUL.

therefore , You can see the following information in the console :

10 month  21, 2020 3:13:30  Afternoon  com.itwanger.Demo main
Information : jcl

How to use JCL The project was converted to use SLF4J What about ?

The third step , Use jcl-over-slf4j.jar Replace commons-logging.jar, And add jul-to-slf4j.jar、slf4j-log4j12.jar( Will automatically introduce slf4j-api.jar and log4j.jar):

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.7.25</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jul-to-slf4j</artifactId>
    <version>1.7.29</version>
</dependency>

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

Step four , stay resources Create under directory log4j.properties file , The contents are shown below :

###  Set up ###
log4j.rootLogger = debug,stdout,D

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

###  Output DEBUG  Log above level to =debug.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = debug.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layou.........

版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢