配置httpclient使用logback

时间:2022-11-01 20:27:52

项目使用maven构建

1.这pom.xml里添加依赖httpclient并排除commons-logging依赖

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.1</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
2. 因为 common-logging是运行时依赖( runtime dependency),需要添加一个commons-logging 对应的SLF4J bridge 

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.5</version>
</dependency>
3. 添加 SLF4J 和logback的依赖

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.11</version>
</dependency>
4.配置logback,  一个简单的logback.xml 文件内容

<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%-5level %msg [%logger{16} %d{HH:mm:ss}]%n</Pattern>
</layout>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
<logger name="org.apache" level="INFO" />
<logger name="org.apache.http.wire" level="DEBUG" />
</configuration>