LoggingInterceptor - Interceptor for OkHttp3 with pretty logger (version for use with Java logging frameworks - jul, log4j, slf4j, logback, log4j2 etc)

wercker status quality gate security coverage FOSSA Status

NB! Library will be archived in the nearest future. Use new library Plinter instead


What is the difference from original repository? Personally I find this interceptor very useful for API testing purposes but original implementation works well only with Android. Had to significantly rewrite original library to use it in pure Java project with log4j2 based logging. So, comparing to original repository, changes are following:

  • removed all Android specific stuff (tags, references to BuildConfig etc)
  • removed included Android app (along with existing tests)
  • removed option to add custom headers / queries
  • updated to Java level 8
  • refactored Interceptor to make it work without any additional configuration (just plug and play) :)
  • fixed some bugs (mostly output related)
  • added option to customize logger output (when default JUL logger is used)
  • removed some useless params (like option to select between JUL info or warning severity, request/response tags etc)
  • added new tests package (can be helpful to figure out how Interceptor should work)
  • added new DefaultLogger implementation (basically just manually configured JUL logger)
  • reworked builder (to support those above mentioned changes)
  • interceptor now pretty prints XML/HTML body
  • max output length can be modified (can be useful for body pretty printing)

Basic Usage

Interceptor should work as is - without any additional parameters. By default JUL logger will be used with INFO level and minimal format displaying message only. okhttp3 version:

    Okhttp3LoggingInterceptor interceptor = new LoggingInterceptor.Builder()                    
    OkHttpClient okHttpClient = new OkHttpClient.Builder()

    // Interceptor can be used with retrofit
    Retrofit retrofitAdapter = new Retrofit.Builder()

okhttp version: (can be used for clients generated from swagger-codegen using okhttp-gson client)

    final OkhttpLoggingInterceptor interceptor = new LoggingInterceptor.Builder()                    
    OkHttpClient okHttpClient = new OkHttpClient.Builder()

apache httpclient version: (can be used for clients generated from swagger-codegen using okhttp-gson client)

    final ApacheHttpRequestInterceptor requestInterceptor = new LoggingInterceptor.Builder()                    
    final ApacheHttpResponseInterceptor responseInterceptor = new LoggingInterceptor.Builder()        

    return HttpClientBuilder
            .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())


Format can be changed to one of the defined templates, for example:

    Okhttp3LoggingInterceptor interceptor = new LoggingInterceptor.Builder()

Tip: when logger is in "message only" mode, json response can be copied from console and converted to POJO with this service in a matter of seconds.

Advanced Usage

Interceptor can be configured to be used with any existing Java logger - just need to provide own LogWriter implementation.

Simple configuration for Log4j2:

    Okhttp3LoggingInterceptor interceptor = new LoggingInterceptor.Builder()
        .logger(new LogWriter() {
          final Logger log = LogManager.getLogger("OkHttpLogger");

          public void log(String msg) {

Or more sophisticated approach with custom logging pattern.

    LogWriter log4j2Writer = new LogWriter() {
      final String OK_HTTP_LOG_PATTERN = "[OkHTTP] %msg%n";
      final Logger log = LogManager.getLogger("OkHttpLogger");

        final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        final Configuration config = ctx.getConfiguration();

        LoggerConfig loggerConfig = new LoggerConfig("OkHttpLogger", Level.TRACE, false);
        PatternLayout layout = PatternLayout

        final Appender appender = ConsoleAppender


        loggerConfig.addAppender(appender, Level.TRACE, null);
        config.addLogger("OkHttpLogger", loggerConfig);

      public void log(String msg) {

    Okhttp3LoggingInterceptor interceptor = new LoggingInterceptor.Builder()




allprojects {
    repositories {
        maven { url 'https://jitpack.io' }

dependencies {
    compile('com.github.dkorobtsov:LoggingInterceptor:4.5') {
            exclude group: 'org.json', module: 'json'





Add executor that allows to perform sequential concurrent print.


Predefined JUL logging patterns:

           .JUL_FULL                // [Date][Thread][Level] Message
           .JUL_DATE_LEVEL_MESSAGE  // [Date][Level] Message
           .JUL_THREAD_MESSAGE      // [Thread] Message
           .JUL_LEVEL_MESSAGE       // [Level] Message
           .JUL_DATE_MESSAGE        // [Date] Message
           .JUL_MESSAGE_ONLY        // Message

Note that given setting works only with default JUL logger.

Line Length

 .maxLineLength(160) // If needed, max output length can be modified. Default value: 110. Valid values: 10-500.


          .NONE       // No logs
          .BASIC      // Logging url, method, headers and body.
          .HEADERS    // Logging url, method and headers
          .BODY       // Logging url, method and body


.loggable(true/false) // enable/disable sending logs output.


FOSSA Status


Captures HTTP client traffic and pretty prints it with any Java logger. Supported clients: OkHttp, OkHttp3, ApacheHttpClient. Body types formatted: json, xml, html.

Logginginterceptor Info

⭐ Stars12
🔗 Source Codegithub.com
🕒 Last Updatea year ago
🕒 Created4 years ago
🐞 Open Issues0
➗ Star-Issue RatioInfinity
😎 Authordkorobtsov