Code Monkey home page Code Monkey logo

alamofireeasylogger's Introduction

AlamofireEasyLogger

Version License Platform

AlamofireEasyLogger is an easy drop-in Alamofire request & response logger. No configuration is needed, it just works.

It works for all requests and responses. There is no need to call a function on every request. It provides a full request/response logger out of the box and allows for easy overriding in case you have different requirements for what you want to see.

Requirements

  • iOS 10.0+
  • Swift 5.0+

For Swift 4.2 please use pod version 1.0.0. For Alamofire 4.6 please use pod version 1.1.0.

Dependencies

Installation

AlamofireEasyLogger is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'AlamofireEasyLogger'

Usage

Out of the box AlamofireEasyLogger provides the FancyAppAlamofireLogger class that logs request and responses with their parameters, headers and bodies as in the example below.

This works for both successful and unsuccessful requests/responses and the output is nicely formatted.

FancyAppAlamofireLogger log example

๐Ÿš€๐Ÿš€๐Ÿš€ REQUEST ๐Ÿš€๐Ÿš€๐Ÿš€
๐Ÿ”ˆ GET https://api.openweathermap.org/data/2.5/forecast?lat=-26.2041028&lon=28.0473051&APPID=hidden&units=metric
๐Ÿš€๐Ÿš€๐Ÿš€ REQUEST ๐Ÿš€๐Ÿš€๐Ÿš€

โœ…โœ…โœ… SUCCESS RESPONSE โœ…โœ…โœ…
๐Ÿ”ˆ GET https://api.openweathermap.org/data/2.5/weather?lat=-26.2041028&lon=28.0473051&APPID=hidden&units=metric
๐Ÿ”ˆ Status code: 200
๐Ÿ’ก Access-Control-Allow-Methods: GET, POST
๐Ÿ’ก X-Cache-Key: /data/2.5/weather?APPID=hidden&lat=-26.2&lon=28.05&units=metric
๐Ÿ’ก Server: openresty
๐Ÿ’ก Content-Length: 441
๐Ÿ’ก Date: Sat, 12 Jan 2019 16:30:50 GMT
๐Ÿ’ก Content-Type: application/json; charset=utf-8
๐Ÿ’ก Access-Control-Allow-Origin: *
๐Ÿ’ก Access-Control-Allow-Credentials: true
๐Ÿ’ก Connection: keep-alive
{"coord":{"lon":28.05,"lat":-26.2},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"base":"stations","main":{"temp":29.53,"pressure":1015,"humidity":37,"temp_min":29,"temp_max":30},"wind":{"speed":0.5,"deg":310},"rain":{"3h":0.575},"clouds":{"all":32},"dt":1547308800,"sys":{"type":1,"id":1958,"message":0.0103,"country":"ZA","sunrise":1547263628,"sunset":1547312706},"id":993800,"name":"Johannesburg","cod":200}
โœ…โœ…โœ… SUCCESS RESPONSE โœ…โœ…โœ…

Usage in app

You need to hold a reference to your logger instance. The place to do this might be your AppDelegate or any other place where the reference will stay alive.

import UIKit
import AlamofireEasyLogger

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    let alamofireLogger = FancyAppAlamofireLogger(
        logFunction: {
            print($0)
        }
    )
    
    ...
}

As you can see above the only parameter the logger takes during initialization is logFunction closure defined as public typealias LogFunction = (String) -> Void. This function is responsible for logging the requests and responses. You can use a simple print as in the example above or integrate a logging library of your choice.

Implementing your own logger

If the FancyAppAlamofireLogger is too verbose for your taste or you want to implement a completely custom logger you can use the AlamofireLoggerDelegate of AlamofireEasyLogger.

Your logger should be used exactly like the FancyAppAlamofireLogger in the example above and should look similar to following piece of code.

class FancyAppAlamofireLogger: AlamofireLoggerDelegate {
    
    let alamofireLogger = AlamofireLogger()
    
    public init() {
        alamofireLogger.delegate = self
        alamofireLogger.startLogging()
    }
    
    open func networkRequestDidStart(request: AlamofireLoggerRequest) {
        ...
    }
    
    open func networkRequestDidComplete(request: AlamofireLoggerRequest, result: AlamofireLoggerResult) {
        ...
    }
    
    open func loggingFailed(error: AlamofireLoggingError) {
        ...
    }
}

As you can see on this example AlamofireEasyLogger is quite versatile and with the use of AlamofireLoggerDelegate you're able to develop a completely custom logger while leveraging the AlamofireLoggerRequest, AlamofireLoggerResult and AlamofireLoggingError entities provided to you.

Author

Jan Kaltoun, [email protected]

License

AlamofireEasyLogger is available under the MIT license. See the LICENSE file for more info.

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.