devcsrj/slf4j-okhttp3-logging-interceptor


An OkHttp logging interceptor for Slf4j loggers

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.devcsrj:slf4j-okhttp3-logging-interceptor:1.0.0'
	}
	dependencies {
		implementation("com.github.devcsrj:slf4j-okhttp3-logging-interceptor:1.0.0")
	}
	<dependency>
	    <groupId>com.github.devcsrj</groupId>
	    <artifactId>slf4j-okhttp3-logging-interceptor</artifactId>
	    <version>1.0.0</version>
	</dependency>

                            
    libraryDependencies += "com.github.devcsrj" % "slf4j-okhttp3-logging-interceptor" % "1.0.0"
        
        

                            
    :dependencies [[com.github.devcsrj/slf4j-okhttp3-logging-interceptor "1.0.0"]]
        
        

Readme


Slf4j Logging Interceptor

An OkHttp interceptor which logs HTTP request and response data to an slf4j logger.

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 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.

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:

    new HttpLoggingInterceptor( yourLogger, 4096L );

Downloading

Get via Maven:

    <dependencies>
        <dependency>
          <groupId>com.github.devcsrj</groupId>
          <artifactId>slf4j-okhttp3-logging-interceptor</artifactId>
          <version>1.0.1</version>
        </dependency>
        ...
    </dependencies>

or via Gradle

   dependencies {
        compile 'com.github.devcsrj:slf4j-okhttp3-logging-interceptor:1.0.1'
   }