Giter Club home page Giter Club logo

alamofireactivitylogger's Introduction

AlamofireActivityLogger

A response serializer for Alamofire which logs both request and response. It provides 4 log levels and a few options to configure your logs.

AlamofireActivityLogger is available for:

  • iOS (from 9.0)
  • macOS (from 10.11)
  • watchOS (from 2.0)
  • tvOS (from 9.0)

NOTE: This version is written for Alamofire 4.x and Swift 3. To support Swift 2 and Alamofire 3.x use the version 1.0.1.

Installing AlamofireActivityLogger

Using CocoaPods

Add the following to your Podfile:

pod 'AlamofireActivityLogger'

Then run $ pod install.

And finally, in the classes where you need AlamofireActivityLogger:

import AlamofireActivityLogger

If you don’t have CocoaPods installed or integrated into your project, you can learn how to do so here.

Log

To log a request you just have to write:

request(.get, url)
    .validate()
    .log()
}

Additionally, you can provide the log level and some options:

request(.get, url)
    .validate()
    .log(level: level, options: options, printer: myPrinter)
}

Let's see the levels and options.

Levels

Are instances of the enum LogLevel. The available values are:

  • none: Do not log requests or responses.

  • all: Logs HTTP method, URL, header fields, & request body for requests, and status code, URL, header fields, response string, & elapsed time for responses.

  • info: Logs HTTP method & URL for requests, and status code, URL, & elapsed time for responses.

  • error: Logs HTTP method & URL for requests, and status code, URL, & elapsed time for responses, but only for failed requests.

The default value is all.

Options

Are instances of the enum LogOption. The available values are:

  • onlyDebug: Only logs if the app is in Debug mode

  • jsonPrettyPrint: Prints the JSON body on request and response

  • includeSeparator: Include a separator string at the begining and end of each section

The default value is [.onlyDebug, .jsonPrettyPrint, .includeSeparator].

Custom printers

If you use a third party logger, you can integrate it with AlamofireActivityLogger. To do it, you must provide a Printer object.

Printer is a protocol which just requires one method:

func print(_ string: String, phase: Phase)

As an example, let’s suppose you have SwiftyBeaver integrated in your app. We can create a SwiftyBeaverPrinter struct like this:

struct SwiftyBeaverPrinter: Printer {
    func print(_ string: String, phase: Phase) {
        if phase.isError {
            log.error(string)
        }
        else {
            log.info(string)
        }
    }
}

And use it this way:

request(.get, url)
    .validate()
    .log(printer: SwiftyBeaverPrinter())
}

By default, a instance of NativePrinter is used. It just make use of the native Swift.print to print the info.

Supported requests

At the moment, AlamofireActivityLogger has support for DataRequest and DownloadRequest. If you need to add support to any other Request subclass, just make it conforms the LoggeableRequest protocol. Take a look at the DataRequest implementation to know how.

Contact

Manuel García-Estañ Martínez
@manueGE

License

AlamofireActivityLogger is available under the MIT license.

alamofireactivitylogger's People

Contributors

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