学习资料
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
如果绑定成功会出现以下日志
否则,可能会出现 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”?>
<Appenders>
<Console name="STDOUT-APPENDER" target="SYSTEM_OUT">
<PatternLayout pattern="%-5p %c{2} - %m%n%throwable" charset="UTF-8"/>
</Console>
<Console name="STDERR-APPENDER" target="SYSTEM_ERR">
<PatternLayout pattern="%-5p %c{2} - %m%n%throwable" charset="UTF-8"/>
</Console>
</Appenders>
<Loggers>
<AsyncLogger name="STDOUT" additivity="false" level="info">
<AppenderRef ref="STDOUT-APPENDER"/>
</AsyncLogger>
<AsyncLogger name="STDERR" additivity="false" level="${ctx:logging.level.com.alipay.antmedia}">
<AppenderRef ref="STDERR-APPENDER"/>
</AsyncLogger>
<AsyncLogger name="com.alipay.${appName}" additivity="false" level="INFO">
<AppenderRef ref="STDOUT-APPENDER" /> -- 代表输出到控制台
</AsyncLogger>
<AsyncRoot level="${ctx:logging.level.com.alipay.antmedia}">
<AppenderRef ref="ERROR-APPENDER"/>
<AppenderRef ref="STDOUT-APPENDER"/>
</AsyncRoot>
</Loggers>
</Configuration>
<a name="AnrlS"></a>
###
<a name="janFu"></a>
### 如何排除间接依赖<br /><br />
1 在总pom所在的目录下执行: mvn clean install -Dmaven.test.skip=true (生产依赖树)<br />2 在总pom所在的目录下执行: mvn dependency:tree -Dincludes=*slf4j-log4j12* (查看间接依赖)<br />3 排除间接依赖,如
```java
<dependency>
<groupId>com.alipay.sofa.runtime</groupId>
<artifactId>sofa-runtime-test</artifactId>
<exclusions>
<exclusion>
<artifactId>
com.springsource.org.apache.commons.logging
</artifactId>
<groupId>org.apache.commons</groupId>
</exclusion>
<exclusion>
<artifactId>
com.springsource.slf4j.org.apache.commons.logging
</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
<exclusion>
<groupId>org.junit</groupId>
<artifactId>com.springsource.org.junit</artifactId>
</exclusion>
</exclusions>
</dependency>