学习资料

http://www.51gjie.com/javaweb/1122.html
https://my.oschina.net/pingpangkuangmo/blog/410224
https://www.liaoxuefeng.com/wiki/1252599548343744/1264739155914176

最佳实践

sl4j + log4j / logback (在加上lombok ,可以直接使用@Sl4j注解)

配置方式:http://www.51gjie.com/javaweb/1122.html

如果绑定成功会出现以下日志image.png
否则,可能会出现 SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”. 这种一般是没有引入log4j这些实际的日志类库,以及和sl4j的对接库。

注意点:

  • 如果为本地测试,log4j的配置文件必须在测试类所在的bundle下,一般为src/main或src/test(由当前类在main还是test目录)下的resource目录
  • 如果为本地测试,log4j的配置文件里一般配置如下 ```java <?xml version=”1.0” encoding=”UTF-8”?>

    1. <Appenders>
    2. <Console name="STDOUT-APPENDER" target="SYSTEM_OUT">
    3. <PatternLayout pattern="%-5p %c{2} - %m%n%throwable" charset="UTF-8"/>
    4. </Console>
    5. <Console name="STDERR-APPENDER" target="SYSTEM_ERR">
    6. <PatternLayout pattern="%-5p %c{2} - %m%n%throwable" charset="UTF-8"/>
    7. </Console>
    8. </Appenders>
    9. <Loggers>
    10. <AsyncLogger name="STDOUT" additivity="false" level="info">
    11. <AppenderRef ref="STDOUT-APPENDER"/>
    12. </AsyncLogger>
    13. <AsyncLogger name="STDERR" additivity="false" level="${ctx:logging.level.com.alipay.antmedia}">
    14. <AppenderRef ref="STDERR-APPENDER"/>
    15. </AsyncLogger>
    16. <AsyncLogger name="com.alipay.${appName}" additivity="false" level="INFO">
    17. <AppenderRef ref="STDOUT-APPENDER" /> -- 代表输出到控制台
    18. </AsyncLogger>
  1. <AsyncRoot level="${ctx:logging.level.com.alipay.antmedia}">
  2. <AppenderRef ref="ERROR-APPENDER"/>
  3. <AppenderRef ref="STDOUT-APPENDER"/>
  4. </AsyncRoot>
  5. </Loggers>
  6. </Configuration>
  1. <a name="AnrlS"></a>
  2. ###
  3. <a name="janFu"></a>
  4. ### 如何排除间接依赖<br /><br />
  5. 1 在总pom所在的目录下执行: mvn clean install -Dmaven.test.skip=true (生产依赖树)<br />2 在总pom所在的目录下执行: mvn dependency:tree -Dincludes=*slf4j-log4j12* (查看间接依赖)<br />3 排除间接依赖,如
  6. ```java
  7. <dependency>
  8. <groupId>com.alipay.sofa.runtime</groupId>
  9. <artifactId>sofa-runtime-test</artifactId>
  10. <exclusions>
  11. <exclusion>
  12. <artifactId>
  13. com.springsource.org.apache.commons.logging
  14. </artifactId>
  15. <groupId>org.apache.commons</groupId>
  16. </exclusion>
  17. <exclusion>
  18. <artifactId>
  19. com.springsource.slf4j.org.apache.commons.logging
  20. </artifactId>
  21. <groupId>org.slf4j</groupId>
  22. </exclusion>
  23. <exclusion>
  24. <artifactId>log4j</artifactId>
  25. <groupId>log4j</groupId>
  26. </exclusion>
  27. <exclusion>
  28. <artifactId>slf4j-log4j12</artifactId>
  29. <groupId>org.slf4j</groupId>
  30. </exclusion>
  31. <exclusion>
  32. <artifactId>commons-logging</artifactId>
  33. <groupId>commons-logging</groupId>
  34. </exclusion>
  35. <exclusion>
  36. <groupId>org.junit</groupId>
  37. <artifactId>com.springsource.org.junit</artifactId>
  38. </exclusion>
  39. </exclusions>
  40. </dependency>