1. Introduction of facade mode

1. Definition: The external and internal communication of a subsystem must be carried out through a unified object. The facade mode provides a high-level interface, making the subsystem easier to use.

In layman’s terms, a class is designed specifically for external services, and the facade object is the only way for the outside world to access the interior of the subsystem.

2. Two roles:

  1. Facade Role: Methods that clients can invoke for this role. This role knows all the functions and responsibilities of the subsystem. Typically, this role delegates all requests from the client to the appropriate subsystem, meaning that this role has no actual business logic and is just a delegate class.

  2. Subsystem Role: Can have one or more subsystems at the same time. Each subsystem is not a single class, but a collection of classes, and the subsystem is unaware of the existence of a facade. To a subsystem, a facade is just another client.

3. Advantages of facade mode:

  1. Reduce system interdependence. If the facade mode is not used, the external access directly goes deep into the subsystem, and there is a strong coupling relationship between each other. Such strong dependence cannot be accepted by system design. The appearance mode is a good solution to this problem. All the dependencies are on the appearance object and have nothing to do with subsystems.

  2. Increased flexibility. With less dependence, flexibility naturally increases;

  3. Improve security. Open those logic to the business that want you to access the subsystem, not open methods on the facade, can not access.

2. Introduction to logging framework

1. Overview: Logging is an essential part of an application, including online problem tracking, log-based statistical analysis of business logic, and so on.

2. Common logging frameworks:

  • Log4j: Apache Log4j is a Java-based logging tool. It was pioneered by Ceki Gulcu and is now a project of the Apache Software Foundation.

  • Log4j2: Apache Log4j2 is an upgrade of Log4j developed by Apache.

  • Logback: a popular logging framework from the authors of Log4J that offers significant performance improvements over log4j.

  • JUL: Java Util Logging, the official Logging implementation since Java1.4.

  • Slf4j: Simple Logging Facade for Java is a Simple Java Logging Facade that does not have a Logging implementation.

  • Commons Logging: A project of the Apache Foundation, it is a set of Java Logging interfaces, formerly called Jakarta Commons Logging, later renamed Commons Logging.

3. Log framework division:

Log facade: Slf4j, JCL (Commons Logging)

Log implementation: Log4j, Log4j2, Logback, JUL

SLF4j is easy to use

Take Slf4j+Logback as an example.

1, rely on

<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> </dependency> < the dependency > < groupId > ch. Qos. Logback < / groupId > < artifactId > logback - core < / artifactId > < version > 1.2.3 < / version > </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> The < version > 1.2.3 < / version > < / dependency >Copy the code

2, configuration,

logback.xml

<? The XML version = "1.0" encoding = "utf-8"? > <configuration> <! <property name="LOG_HOME" value="/logs" /> <! - the console output - > < appender name = "STDOUT" class = "ch. Qos. Logback. Core. ConsoleAppender" > < encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <! Format output: %d indicates the date, %thread indicates the thread name, %-5level indicates the level from the left 5 character width % MSG: Log messages, % n is a newline -- > < pattern > % d {HH: mm: ss. The SSS} - 5 level thread] [% % % logger {50} - # # # # # # # # # # # # # % MSG % n < / pattern > < / encoder > </appender> <! Generated according to the daily log FILE - > < appender name = "FILE" class = "ch. Qos. Logback. Core. Rolling. RollingFileAppender" > < rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <! ${LOG_HOME}/mallweb.log.%d{YYYY-MM-DD}. Log </FileNamePattern> <! -- Log file retention days --> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <! Format output: %d indicates the date, %thread indicates the thread name, %-5level indicates the level from the left 5 character width % MSG: -> <pattern>%d{YYYY-MM-DD HH: MM :ss.SSS} [%thread] %-5level % Logger {50} - % MSG %n</pattern> </ coder> <! -- the largest size in the log file -- > < triggeringPolicy class = "ch. Qos. Logback. Core. Rolling. SizeBasedTriggeringPolicy" > <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <logger name="com.xwtech.mnoframework" level="debug" /> <logger name="org.springframework" level="INFO" /> <logger name="com.alibaba.druid" level="INFO" /> <! --myibatis log configure --> <logger name="org.apache.ibatis" level="debug" /> <logger name="java.sql.Connection" level="INFO" /> <logger name="java.sql.Statement" level="INFO" /> <logger name="java.sql.PreparedStatement" level="INFO"  /> <logger name="log4j.logger.net" level="ERROR"/> <logger name="log4j.logger.net.spy.memcached.transcoders.SerializingTranscoder" level="ERROR"/> <! Appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> </root> </configuratiCopy the code

3, test,

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) public class LogTest { private static Logger logger = LoggerFactory.getLogger(LogTest.class); @Test public void test() { logger.error("error"); logger.debug("debug"); logger.info("info"); logger.trace("trace"); logger.warn("warn"); }}Copy the code

Fourth, SLF4j integration with common logging frameworks

<! Integrate other logging frameworks --> <! -- slf4j + log4j --> <! -- contains log4j implementation, Slf4j </groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.21</version>  </dependency> <! Log4j --> <dependency> <groupId>log4j</groupId> <artifactId> <version>1.2.16</version> </dependency> <! -- slf4j + log4j2 --> <! -- log42 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> < version > 2.12.1 < / version > < / dependency > < the dependency > < groupId > org. Apache. Logging. Log4j < / groupId > < < artifactId > log4j - API/artifactId > < version > 2.12.1 < / version > < / dependency > <! -- slf4j + java.util.logging --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> The < version > 1.7.21 < / version > < / dependency > <! -- slf4j + jboss-logging --> <dependency> <groupId>org.jboss.slf4j</groupId> < artifactId > slf4j - jboss - logging < / artifactId > < version > 1.2.0. The Final < / version > < / dependency > <! -- slf4j + logback --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> < version > 1.2.3 < / version > < / dependency > < the dependency > < groupId > ch. Qos. Logback < / groupId > The < artifactId > logback - classic < / artifactId > < version > 1.2.3 < / version > < / dependenCopy the code

How to use SLF4j for log output

<! -- Unified log output --> <! Slf4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> The < version > 1.7.21 < / version > < / dependency > <! -- log4j2 delegate to slf4J --> <dependency> <groupId>org.apache.logging. Log4j </groupId> <artifactId>log4j-to-slf4j</artifactId> The < version > 2.9.1 < / version > < / dependency > <! -- Java.util. logging Delegate to slf4J --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jul-to-slf4j</artifactId> The < version > 1.7.21 < / version > < / dependency > <! -- replace org.apache.com mons. Logging, <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> The < version > 1.7.21 < / version > < / dependencCopy the code

6. Frequently asked Questions

1. No logging implementation

2. Multi-log implementation

3. Stack overflow

If you have both a bridge and an adapter in clappSTH, the log gets kicked around in an endless loop

Develop log protocol in manual

Reference: www.cnblogs.com/xrq730/p/86…

Reference: www.jianshu.com/p/3dff7301f…