https://github.com/devcsrj/slf4j-okhttp3-logging-interceptor
An OkHttp logging interceptor for Slf4j loggers
https://github.com/devcsrj/slf4j-okhttp3-logging-interceptor
logging okhttp3 slf4j
Last synced: 6 months ago
JSON representation
An OkHttp logging interceptor for Slf4j loggers
- Host: GitHub
- URL: https://github.com/devcsrj/slf4j-okhttp3-logging-interceptor
- Owner: devcsrj
- License: apache-2.0
- Created: 2017-02-17T02:23:05.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-27T15:01:23.000Z (almost 8 years ago)
- Last Synced: 2025-03-25T06:40:35.504Z (6 months ago)
- Topics: logging, okhttp3, slf4j
- Language: Java
- Size: 32.2 KB
- Stars: 4
- Watchers: 1
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Slf4j Logging Interceptor
An [OkHttp interceptor][1] which logs HTTP request and response data to an [slf4j logger](http://www.slf4j.org/).
```java
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new HttpLoggingInterceptor())
.build();
```You can change the log level at any time on your corresponding logging configuration. For example, in [log4j](https://logging.apache.org/log4j/2.x/) you'll use:
```
log4j.logger.okhttp3.logging.wire=DEBUG
```
The interceptor logs based from the following rules:- `DEBUG` - request and response lines and their respective headers and bodies
- `INFO` - request and response lines and their respective headers
- `ERROR`/`WARN` - none## Customization
To log to a custom location, pass a `Logger` instance to the constructor.
```java
private static final Logger yourLogger = LoggerFactory.getLogger( YourClass.class );...
HttpLoggingInterceptor logging = new HttpLoggingInterceptor( yourLogger );
```> Don't forget to update your logging properties as well!
**Warning**: The logs generated by this interceptor when using the `DEBUG` or `INFO` levels has the potential to leak sensitive information such as "Authorization" or "Cookie" headers and the contents of request and response bodies. This data should only be logged in a controlled way or in a non-production environment.
At `DEBUG` level, the interceptor by default loads the **entire** response body prior logging. This ensures that the `ResponseBody` is kept readable after the interceptor is finished. On cases where the expected content is huge, you can limit the size through one of it's constructor arguments:
```java
new HttpLoggingInterceptor( yourLogger, 4096L );
```## Downloading
Get via Maven:
```xml
com.github.devcsrj
slf4j-okhttp3-logging-interceptor
1.0.1
...
```or via Gradle
```groovy
dependencies {
compile 'com.github.devcsrj:slf4j-okhttp3-logging-interceptor:1.0.1'
}
```[1]: https://github.com/square/okhttp/wiki/Interceptors