AlamofireEasyLogger 1.6.0

AlamofireEasyLogger 1.6.0

Jan Kaltoun 维护。



  • jankaltoun

AlamofireEasyLogger

Version License Platform

AlamofireEasyLogger 是一个简单的 Alamofire 请求与响应记录器。无需配置,直接使用即可。

它适用于所有请求和响应。无需对每个请求调用函数。它提供了一款内建的全请求/响应记录器,并允许轻松覆盖以满足不同的查看需求。

需求

  • iOS 10.0+
  • Swift 5.0+

对于 Swift 4.2,请使用 pod 版本 1.0.0。 对于 Alamofire 4.6,请使用 pod 版本 1.1.0。

依赖关系

安装

Swift 包管理器

只需将此仓库的 URL 添加到您的包依赖中。

https://github.com/jankaltoun/AlamofireEasyLogger

Cocoapods

要使用 Cocoapods 安装此库,只需在 Podfile 中添加以下行。

pod 'AlamofireEasyLogger'

Carthage

要使用 Carthage 集成此库,请将以下行添加到 Cartfile。

github "jankaltoun/AlamofireEasyLogger"

用法

AlamofireEasyLogger 默认提供 FancyAppAlamofireLogger 类,用于记录请求及响应的参数、头和体,如下例所示。

这适用于成功和不成功的请求/响应,并且输出格式良好。

FancyAppAlamofireLogger 日志示例

🚀🚀🚀 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 ✅✅✅

在应用中使用

您需要保留对日志实例的引用。完成这项工作的位置可能是您的 AppDelegate 或任何引用将保持存在的其他位置。

import UIKit
import AlamofireEasyLogger

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

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

如您所见,初始化日志记录器时,它只接受一个参数,即logFunction闭包,定义为public typealias LogFunction = (String) -> Void。此功能负责记录请求和响应。您可以像上面示例中所做的那样使用简单的print,或者集成您选择的自定义日志库。

实现自定义日志记录器

如果FancyAppAlamofireLogger过于冗长,或者您想实现一个完全自定义的日志记录器,您可以使用AlamofireEasyLoggerAlamofireLoggerDelegate

您的日志记录器应与上述示例中的FancyAppAlamofireLogger的使用方式完全相同,并应类似以下代码段。

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) {
        ...
    }
}

如您在此示例中看到,AlamofireEasyLogger非常灵活,通过使用AlamofireLoggerDelegate,您能够开发一个完全自定义的日志记录器,同时利用提供的AlamofireLoggerRequestAlamofireLoggerResultAlamofireLoggingError实体。

作者

Jan Kaltoun, [email protected]

许可

请随意使用此代码。

如果您链接到此存储库或提及我是作者,我会很高兴!