Problem 1.

This problem is caused by the fact that the company used SLF4J to print logs during the internship, while in the previous lab project, the company used the API of log4j directly to print logs. I didn’t pay much attention at first, until I saw that the logging protocol enforced SLF4J.

2.SLF4J

SLF4J (Simple Loging Facade For Java) is a logging abstraction layer that allows you to use any logging system and switch between them without having to touch a written application. Slf4j alone does not work, it must be used with other specific logging implementations such as Apache org.apache.log4j.Logger, java.util.logging.logger and so on.

2.1 Relationship between SLF4J and Log4J

2.2 the advantages

  • Succinct placeholders use place holders, properties represented as “{}” in code.

The placeholder is very similar to %s in the format() method of string, because it is replaced at run time by some supplied actual string. This not only reduces the number of string concatenations in your code, but also saves on creating new strings.

// Common log needs to be concatenated
log.info("Set score " + score + " for Person " + p.getName() + " ok.");
//Slf4j 
logger.info(ClassName method error: {}, MSG: {}",response.get("code"),response.get("message"));
Copy the code
  • Log levels: Info Error WARN DEBUG Trace…
logger.info();
logger.error();
logger.warn();
Copy the code

3. Adaptor pattern

3.1 What is the adaptor pattern

Blog.csdn.net/lovejavasma… The adapter is used to find the appropriate class method for the target or to modify (rewrite) the found method to complete the method invocation

  • Object adapters depend on object composition

Target: The Target interface that requires an implementation of the method2 method

Adaptee: An Adaptee (which can be a class or interface) with only one method1 method

Adapter: An Adapter that implements the Target interface and inherits the Adaptee method

The demo reference: https://blog.csdn.net/caoxiaohong1005/article/details/53445968Copy the code

3.2 Application of SLF4J

1.getLogger

2.LoggerFactory

/ / ILoggerFactory for target
public static Logger getLogger(String name) {
        ILoggerFactory iLoggerFactory = getILoggerFactory();
        return iLoggerFactory.getLogger(name);
    }

    public static Logger getLogger(Class clazz) {
        return getLogger(clazz.getName());
    }
    
 / / ILoggerFactory interface
import org.slf4j.Logger;
public interface ILoggerFactory {
    Logger getLogger(String var1);
}
    
Copy the code

GetILoggerFactory: The different getLogger methods correspond to an implementation class called Adapter

3. First Adapter: Log4jLoggerFactory

Implement ILoggerFactory interface getLogger method

public class Log4jLoggerFactory implements ILoggerFactory {
    ConcurrentMap<String, Logger> loggerMap = new ConcurrentHashMap();

    public Log4jLoggerFactory() {
    }

    public Logger getLogger(String name) {
        Logger slf4jLogger = (Logger)this.loggerMap.get(name);
        if(slf4jLogger ! = null) {return slf4jLogger;
        } else {
            org.apache.log4j.Logger log4jLogger;
            if(name.equalsIgnoreCase("ROOT")) {
                log4jLogger = LogManager.getRootLogger();
            } else {
                log4jLogger = LogManager.getLogger(name); } // Create onelogLog4jLoggerAdapter newInstance = new Log4jLoggerAdapter(log4jLogger);
            Logger oldInstance = (Logger)this.loggerMap.putIfAbsent(name, newInstance);
            return(Logger)(oldInstance == null? newInstance:oldInstance); }}}Copy the code

Important reference: blog.csdn.net/caoxiaohong…

4. To summarize

  • SLF4J can dynamically select different loggers
  • SLF4J principle implementation uses adapter pattern, factory pattern, singleton pattern
  • A common use of the Adapter pattern: merge Adapters for common code