Java logging system

Log pivotal in the system, especially for systems already on the line, is the key to locate the problem. Daily development System.out print log can be used to quickly view runtime information, it is possible for the online system, more information is needed, such as: time, class and method names printed log is located and centralized control switch log printing, System.out obviously can not meet the requirements. Thus developed the following logging framework, in order to facilitate the migration log system, Java logging framework mimic JDBC proposed Java facade, but did not reach a unified, each frame has its own facade, general logging frameworks are from API + Imp two parts.

Logs development

1. Apache Log4j

Apache Log4j 2 is an upgrade to Log4j that provides significant improvements over its predecessor, Log4j 1.x, and provides many of the improvements available in Logback while fixing some inherent problems in Logback’s architecture.

Log4j is Apache company's products, mainstream version is Log4j 2, the Facade has its own logging: log4j-API , and therefore need to use the following dependence

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.13.0</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.13.0</version>
  </dependency>
</dependencies>

Detailed configuration is available from the official website to find, use the following:

import org.apache.log4j.Logger;
...
Logger logger = Logger.getLogger(Test.class);
logger.trace("trace");

2. Commons-Logging

Sun and Apache to compete, but also developed a logging tool, built under the JDK java.util.logging package, generally referred to as JCL. Appeared on the market so that different logging framework, in order to unify, Jakarta drawn up a unified interface java commons logging. This logging framework developer oriented programming Facade, logging framework calls this Facade user code (similar to the JDBC), while at Runtime, implemented according to the implementation of different logs of different configurations.

3. Logback+slf4j

Gradually, Log4j can not meet the demand, the author has developed a new log to achieve LogBack and the Facade the Log SLF4J .

1. dependence

<!-- LogBack -->
<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <version>1.1.7</version>
</dependency>

2. Configure logback.xml file

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
        </layout>
    </appender>
    <logger name="com.chanshuyi" level="TRACE"/>
 
    <root level="warn">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

3. Call

import org.slf4j.Logger;    // 注意导入的包是slf4j
import org.slf4j.LoggerFactory;
...
    Logger logger = LoggerFactory.getLogger(Slf4jJDKLog.class);
    logger.trace("Trace Level.");
    logger.info("Info Level.");
    logger.warn("Warn Level.");
    logger.error("Error Level.");

At this point Java logging framework mainstream market is Log4j + Log4j-api and logback + slf4j

Best Practices

For new projects, the general use logback + slf4j logging framework. That reliance on third parties to use the log4j or jul how to do, how to unify the output? The answer is that bridge .

  1. Logging framework used depends on the project

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.21</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.1.7</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>1.1.7</version>
    </dependency>
  2. Bridge-dependent, and the jul log4j redirected to SLF4J, so real log is used in this project slf4j + logback logging framework

    <!-- https://mvnrepository.com/artifact/org.slf4j/jcl-over-slf4j -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.7.25</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.slf4j/log4j-over-slf4j -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
        <version>1.7.25</version>
    </dependency>

These are the technical project selection is logback + slf4j, if it is the other?

only need to

  1. Determine the log Facade, the general is slf4j
  2. Determine the logging implementation framework, different logging framework for slf4j provide binder, that can be used with most of the logging implementation slf4j use
    • slf4j-jdk14: slf4j to bridge the jdk-logging
    • slf4j-log4j12: slf4j to the bridge log4j1
    • log4j-slf4j-impl:slf4j到log4j2的桥梁这是apache实现,依赖的log4j版本较新。slf4j-log4j12这是slf4j中实现,依赖的log4j版本较低。实验可以互相替代,但是注意配置文件的不同。
    • logback-classic:slf4j到logback的桥梁
    • slf4j-jcl:slf4j到commons-logging的桥梁
  3. 管理依赖项目中的日志框架,拦截且重定向到slf4j中
    • jul-to-slf4j:jdk-logging到slf4j的桥梁
    • log4j-over-slf4j:log4j1到slf4j的桥梁
    • jcl-over-slf4j:commons-logging到slf4j的桥梁

案例二:如何让Spring以log4j2形式输出

spring默认使用的是jcl输出日志

而你的应用中,采用了slf4j+log4j-core,即log4j2进行日志记录

方案一:jcl-over-slf4j适配器

方案二:JUL to SLF4J bridge

但是注意需要在调用日志之前执行以下代码,才能正常工作。

SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();

循环引用问题

想象这样一种情况,通过桥接器将log4j的日志全部重定向到slf4j,然后slf4j又再次绑定到了log4j实现。死循环了。

参考:
1 Java日志框架那些事儿
2 带你弄清混乱的JAVA日志体系
3 apache log4j官网
4 slf4j

Guess you like

Origin www.cnblogs.com/hitomeng/p/12157255.html