Giter Club home page Giter Club logo

logginginterceptor's Introduction

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

NB! Library is archived. Please use new library Plinter instead

Description

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()                    
                    .buildForOkhttp3();
    OkHttpClient okHttpClient = new OkHttpClient.Builder()
        .addInterceptor(interceptor)
        .build();

    // Interceptor can be used with retrofit
    Retrofit retrofitAdapter = new Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .baseUrl("https://.../")
        .client(okHttpClient)
        .build();

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

    final OkhttpLoggingInterceptor interceptor = new LoggingInterceptor.Builder()                    
                    .buildForOkhttp();
    OkHttpClient okHttpClient = new OkHttpClient.Builder()
        .addInterceptor(interceptor)
        .build();

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

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

    return HttpClientBuilder
            .create()
            .addInterceptorFirst(requestInterceptor)
            .addInterceptorFirst(responseInterceptor)
            .setMaxConnTotal(MAX_IDLE_CONNECTIONS)
            .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
            .build();    

Example:

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

    Okhttp3LoggingInterceptor interceptor = new LoggingInterceptor.Builder()
        .loggable(isDebug())
        .level(Level.BASIC)
        .format(LogFormatter.JUL_DATE_LEVEL_MESSAGE)
        .maxLineLength(160)
        .executor(Executors.newSingleThreadExecutor())
        .buildForOkhttp3();

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");

          @Override
          public void log(String msg) {
            log.debug(msg);
          }
        })
        .buildForOkhttp3();

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
            .newBuilder()
            .withPattern(OK_HTTP_LOG_PATTERN)
            .build();

        final Appender appender = ConsoleAppender
            .newBuilder()
            .withName("OkHttpConsoleAppender")
            .withLayout(layout)
            .build();

        appender.start();

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

      @Override
      public void log(String msg) {
        log.debug(msg);
      }
    };

    Okhttp3LoggingInterceptor interceptor = new LoggingInterceptor.Builder()
        .logger(log4j2Writer)
        .buildForOkhttp3();

Example:

Download

Gradle:

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

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

Maven:

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

<dependency>
	    <groupId>com.github.dkorobtsov</groupId>
	    <artifactId>LoggingInterceptor</artifactId>
	    <version>4.5</version>
</dependency>

Executor

Add executor that allows to perform sequential concurrent print.

Format

Predefined JUL logging patterns:

.format(LogFormatter.JUL_DATE_LEVEL_MESSAGE)
           .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.

Level

.setLevel(Level.BASIC)
	      .NONE       // No logs
	      .BASIC      // Logging url, method, headers and body.
	      .HEADERS    // Logging url, method and headers
	      .BODY       // Logging url, method and body

Loggable

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

License

FOSSA Status

logginginterceptor's People

Contributors

azakordonets avatar dkorobtsov avatar fossabot avatar gturedi avatar ihsanbal avatar mrartcore avatar smelfungus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.