An open API service indexing awesome lists of open source software.

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

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