一些java 日志实践

本篇文章不设置git仓库

 

1 log4j2与logback并存,slf4j选哪个?

log4j2对应于slf4j的实现(桥接)为:

log4j-slf4j-impl

同时注入logback以及log4j2的实现

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.13.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.13.3</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.13.3</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.2</version>
        </dependency>

 

log4j2.xml scef-app-log4j2.log

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN" monitorInterval="30">
    <appenders>
        <console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n"/>
        </console>

        <File name="scef-app" fileName="/iem/tomcat/scef/catalina/log4j2/scef-app-log4j2.log" append="true">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n"/>
        </File>
    </appenders>

    <loggers>
        <root level="info">
            <appender-ref ref="Console"/>
            <appender-ref ref="scef-app"/>
        </root>
    </loggers>
</configuration>

  

logback.xml scef-app-logback.log

<configuration>

    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>/iem/tomcat/scef/catalina/log4j2/test-logback.log</file>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration>

  

 

public class TestLog {
    private static final org.slf4j.Logger loggerSlf4j = org.slf4j.LoggerFactory.getLogger(TestLog.class);
    public static void main(String [] f) {
        loggerSlf4j.info("slf4j api");
    }
}

  

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/ys00674/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.13.3/log4j-slf4j-impl-2.13.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/ys00674/.m2/repository/ch/qos/logback/logback-classic/1.1.2/logback-classic-1.1.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]    这个是在log4j-slf4j-impl中的类
2021-02-08 13:57:34.883 [main] INFO log.TestLog - slf4j api

 

显示日志打到了log4j2的配置中

 

追加 -XX:+TraceClassLoading

SLF4J: Class path contains multiple SLF4J bindings.
[Loaded java.util.LinkedHashMap$LinkedKeySet from C:\Program Files\Java\jdk1.8.0_221\jre\lib\rt.jar]
[Loaded java.util.LinkedHashMap$LinkedHashIterator from C:\Program Files\Java\jdk1.8.0_221\jre\lib\rt.jar]
[Loaded java.util.LinkedHashMap$LinkedKeyIterator from C:\Program Files\Java\jdk1.8.0_221\jre\lib\rt.jar]
SLF4J: Found binding in [jar:file:/C:/Users/ys00674/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.13.3/log4j-slf4j-impl-2.13.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/ys00674/.m2/repository/ch/qos/logback/logback-classic/1.1.2/logback-classic-1.1.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
[Loaded org.slf4j.spi.LoggerFactoryBinder from file:/C:/Users/ys00674/.m2/repository/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar]
[Loaded org.slf4j.impl.StaticLoggerBinder from file:/C:/Users/ys00674/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.13.3/log4j-slf4j-impl-2.13.3.jar]

 

显示slf4j先查找org.slf4j.impl.StaticLoggerBinder,再加载

 

Multiple bindings were found on the class path

SLF4J API is designed to bind with one and only one underlying logging framework at a time. If more than one binding is present on the class path, SLF4J will emit a warning, listing the location of those bindings.

When multiple bindings are available on the class path, select one and only one binding you wish to use, and remove the other bindings. For example, if you have both slf4j-simple-2.0.0-alpha0.jar and slf4j-nop-2.0.0-alpha0.jar on the class path and you wish to use the nop (no-operation) binding, then remove slf4j-simple-2.0.0-alpha0.jar from the class path.

The list of locations that SLF4J provides in this warning usually provides sufficient information to identify the dependency transitively pulling in an unwanted SLF4J binding into your project. In your project's pom.xml file, exclude this SLF4J binding when declaring the unscrupulous dependency. For example, cassandra-all version 0.8.1 declares both log4j and slf4j-log4j12 as compile-time dependencies. Thus, when you include cassandra-all as a dependency in your project, the cassandra-all declaration will cause both slf4j-log4j12.jar and log4j.jar to be pulled in as dependencies. In case you do not wish to use log4j as the the SLF4J backend, you can instruct Maven to exclude these two artifacts as shown next:

<dependencies>
  <dependency>
    <groupId> org.apache.cassandra</groupId>
    <artifactId>cassandra-all</artifactId>
    <version>0.8.1</version>

    <exclusions>
      <exclusion>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
      </exclusion>
      <exclusion>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
      </exclusion>
    </exclusions>

  </dependency>
</dependencies>

NOTE The warning emitted by SLF4J is just that, a warning. Even when multiple bindings are present, SLF4J will pick one logging framework/implementation and bind with it. The way SLF4J picks a binding is determined by the JVM and for all practical purposes should be considered random. As of version 1.6.6, SLF4J will name the framework/implementation class it is actually bound to.

Embedded components such as libraries or frameworks should not declare a dependency on any SLF4J binding but only depend on slf4j-api. When a library declares a compile-time dependency on a SLF4J binding, it imposes that binding on the end-user, thus negating SLF4J's purpose. When you come across an embedded component declaring a compile-time dependency on any SLF4J binding, please take the time to contact the authors of said component/library and kindly ask them to mend their ways.

 

slf4j使用哪个实现是随机的

 

 

2 log4j - log4j2

log4j.properties  scef-app-log4j

log4j.rootLogger=INFO,FILE

CATALINA_LOGS=/iem/tomcat/scef/catalina/log4j2

log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.file=${CATALINA_LOGS}/scef-app-log4j.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern= %d [%t] %-5p [%13F:%L]: %m%n
log4j.appender.FILE.append=true
log4j.appender.FILE.bufferedIO=false
log4j.appender.FILE.maxBackupIndex=30
log4j.appender.FILE.maxFileSize=200MB

  

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-1.2-api</artifactId>
            <version>2.12.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.13.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.13.3</version>
        </dependency>

  

public class TestLog {
    private static final org.apache.log4j.Logger loggerLog4j = org.apache.log4j.Logger.getLogger(TestLog.class);
    public static void main(String [] f) {
        loggerLog4j.info("log4j api");
    }
}

  

编译成功,-XX:+TraceClassLoading运行

结果显示,日志打入log4j2.xml 的配置

 

[Loaded org.apache.log4j.Logger from file:/C:/Users/ys00674/.m2/repository/org/apache/logging/log4j/log4j-1.2-api/2.12.1/log4j-1.2-api-2.12.1.jar]

。。。。。。
[Loaded org.apache.logging.log4j.Logger from file:/C:/Users/ys00674/.m2/repository/org/apache/logging/log4j/log4j-api/2.13.3/log4j-api-2.13.3.jar]

 

显示log4j-1.2-api-2.12.1的org.apache.log4j.Logger参与编译,并完成桥接

 

 

 

3 log4j slf4j log4j2 logback并存

public class TestLog {
    private static final org.apache.log4j.Logger loggerLog4j = org.apache.log4j.Logger.getLogger(TestLog.class);
    private static final org.slf4j.Logger loggerSlf4j = org.slf4j.LoggerFactory.getLogger(TestLog.class);
    public static void main(String [] f) {
        loggerLog4j.info("log4j api");
        loggerSlf4j.info("slf4j api");
    }
}

  

 <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>apache-log4j-extras</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>com.documentum</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.13.1</version>
        </dependency>

<!--        <dependency>-->
<!--            <groupId>org.apache.logging.log4j</groupId>-->
<!--            <artifactId>log4j-1.2-api</artifactId>-->
<!--            <version>2.12.1</version>-->
<!--        </dependency>-->

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.13.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.13.3</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.13.3</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.2</version>
        </dependency>

  

结果显示slf4j打到log4j2,log4j打到log4j properties自己

 [Loaded org.apache.log4j.Logger from file:/C:/Users/ys00674/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar] —— 碰巧加载了这个包的org.apache.log4j.Logger

 

4 log4j log4j-log4j2桥接 slf4j log4j2 logback并存

 

<dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>apache-log4j-extras</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>com.documentum</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.13.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-1.2-api</artifactId>
            <version>2.12.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.13.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.13.3</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.13.3</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.2</version>
        </dependency>

  

结果显示仍然 log4j打到自己,slf4j打到log4j2

[Loaded org.apache.log4j.Logger from file:/C:/Users/ys00674/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar]

<dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>apache-log4j-extras</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>com.documentum</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.13.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-1.2-api</artifactId>
            <version>2.12.1</version>
        </dependency>

  

这4个包中都有org.apache.log4j.Logger,碰巧加载到第一个;日志显示log4j-1.2-api全程没被加载

log4j-log4j2桥接包加了白家,压根没加载到

 

5 一个一个试log4j,第1个 3、4已经涉及,第4个2涉及

<!--        <dependency>-->
<!--            <groupId>log4j</groupId>-->
<!--            <artifactId>log4j</artifactId>-->
<!--            <version>1.2.17</version>-->
<!--        </dependency>-->

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>apache-log4j-extras</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>com.documentum</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.13.1</version>
        </dependency>

<!--        <dependency>-->
<!--            <groupId>org.apache.logging.log4j</groupId>-->
<!--            <artifactId>log4j-1.2-api</artifactId>-->
<!--            <version>2.12.1</version>-->
<!--        </dependency>-->

  

[Loaded org.apache.log4j.Logger from file:/C:/Users/ys00674/.m2/repository/log4j/apache-log4j-extras/1.2.17/apache-log4j-extras-1.2.17.jar]

成功打入log4j。properties

 

<!--        <dependency>-->
<!--            <groupId>log4j</groupId>-->
<!--            <artifactId>log4j</artifactId>-->
<!--            <version>1.2.17</version>-->
<!--        </dependency>-->

<!--        <dependency>-->
<!--            <groupId>log4j</groupId>-->
<!--            <artifactId>apache-log4j-extras</artifactId>-->
<!--            <version>1.2.17</version>-->
<!--        </dependency>-->

        <dependency>
            <groupId>com.documentum</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.13.1</version>
        </dependency>

<!--        <dependency>-->
<!--            <groupId>org.apache.logging.log4j</groupId>-->
<!--            <artifactId>log4j-1.2-api</artifactId>-->
<!--            <version>2.12.1</version>-->
<!--        </dependency>-->

  

[Loaded org.apache.log4j.Logger from file:/C:/Users/ys00674/.m2/repository/com/documentum/log4j/1.2.13.1/log4j-1.2.13.1.jar]

成功打入log4j。properties

 

 

 

完整dependency:

<dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>apache-log4j-extras</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>com.documentum</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.13.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-1.2-api</artifactId>
            <version>2.12.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.13.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.13.3</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.13.3</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.2</version>
        </dependency>

    </dependencies>

  

public class TestLog {
    private static final org.apache.log4j.Logger loggerLog4j = org.apache.log4j.Logger.getLogger(TestLog.class);
    private static final org.slf4j.Logger loggerSlf4j = org.slf4j.LoggerFactory.getLogger(TestLog.class);
    public static void main(String [] f) {
        loggerLog4j.info("log4j api");
        loggerSlf4j.info("slf4j api");
    }
}

  

 6 项目

log4j2实现

[?[1;34mINFO?[m] | +- org.apache.logging.log4j:log4j-api:jar:2.12.1:compile
[?[1;34mINFO?[m] | +- org.apache.logging.log4j:log4j-core:jar:2.12.1:compile
[?[1;34mINFO?[m] | +- org.apache.logging.log4j:log4j-web:jar:2.12.1:runtime

 

slf4j-log4j2
[?[1;34mINFO?[m] | \- org.apache.logging.log4j:log4j-slf4j-impl:jar:2.12.1:compile

 

log4j-log4j2

[?[1;34mINFO?[m] +- org.apache.logging.log4j:log4j-1.2-api:jar:2.12.1:compile

 

slf4j api

[?[1;34mINFO?[m] | +- org.slf4j:slf4j-api:jar:1.7.30:compile

 

jcl-slf4j
[?[1;34mINFO?[m] | +- org.slf4j:jcl-over-slf4j:jar:1.7.30:compile

 

jul-slf4j
[?[1;34mINFO?[m] | +- org.slf4j:jul-to-slf4j:jar:1.7.30:compile

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

 实践中,有第二句就行了https://blog.csdn.net/lbh199466/article/details/112161391

posted on 2021-02-08 14:02  silyvin  阅读(181)  评论(0编辑  收藏  举报