T21LoggerSwift 2.1.0

T21LoggerSwift 2.1.0

Marcos MoleroSalvador Martin 维护。



  • Eloi Guzman Ceron

T21Logger

SwiftyBeaver 的包装器

Carthage compatible CocoaPods compatible Swift compatible Platform compatible License SwiftPM

T21Logger 类是一个简单的包装类,用于 SwiftyBeaver 公共第三方日志库。

入门

以下步骤将在测试和开发目的下在您的本地计算机上运行项目副本。

安装

T21Logger 通过 CarthageCocoaPodsSwift 包管理器 提供。

Carthage

要使用 Carthage 安装 T21Logger,请将以下行添加到您的 Cartfile

github "worldline-spain/T21LoggerSwift"

然后运行命令 carthage update --no-use-binaries 或直接运行 carthage update。有关 Carthage 的安装和使用细节,请访问其项目页面

CocoaPods

要使用CocoaPods安装T21Logger,请将以下行添加到您的Podfile中。

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0' 
use_frameworks!

pod 'T21LoggerSwift'

然后运行pod install命令。有关CocoaPods的安装和使用详情,请访问其官方网站

Swift Package Manager

要使用Apple的Swift Package Manager集成,请将以下内容作为依赖项添加到您的Package.swift中。

.package(url: "https://github.com/worldline-spain/T21LoggerSwift.git", .upToNextMajor(from: "2.1.0"))

有关Swift Package Manager的安装和使用详情,请访问其官方网站

如何使用

添加上下文

日志包装器提供了一个简单的方法来为不同的日志添加与上下文相关的字符串。以下示例中我们创建了3个不同的日志记录器:

  • 用于记录应用程序的通用消息。
  • 用于记录与Realm相关查询和内容的消息。
  • 用于记录与NSURLSession相关请求等消息。

创建日志只需创建一个T21Logger实例即可。

let customLogger = T21Logger()
let customLoggerRealm = T21Logger("REALM")
let customLoggerHTTPRequester = T21Logger("NETWORK")

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    customLogger.verbose("App verbose message")
    customLogger.debug("App verbose message")
    customLoggerRealm.info("Realm error message")
    customLoggerHTTPRequester.warning("Network warning message")
    customLoggerRealm.error("Realm error message")

    return true
}

生成的输出如下所示:

💭18:21:04.541 VERBOSE AppDelegate.application():22 - [APP] App verbose message
✅18:21:04.545 DEBUG AppDelegate.application():23 - [APP] App verbose message
ℹ️18:21:04.545 INFO AppDelegate.application():24 - [REALM] Realm error message
⚠️18:21:04.546 WARNING AppDelegate.application():25 - [NETWORK] Network warning message
🚫18:21:04.546 ERROR AppDelegate.application():26 - [REALM] Realm error message

这使得为您的应用程序和内部库维护不同类型的日志变得非常简单。每个日志记录器都将创建自己的日志,这样您就可以轻松地看到日志的来源。

创建一个静态类以保存您的Logger

以下AppLogger.swift类只是一个静态方法的集合,以避免需要处理T21Logger实例。

import Foundation

public class AppLogger {

//MARK: Public methods
public class func verbose(_ message: String?, _ file: String = #file, _ function: String = #function, line: Int = #line) {
    sharedInstance.verbose(message,file,function, line)
}

public class func debug(_ message: String?, _ file: String = #file, _ function: String = #function, line: Int = #line) {
    sharedInstance.debug(message,file,function, line)
}

public class func info(_ message: String?, _ file: String = #file, _ function: String = #function, line: Int = #line) {
    sharedInstance.info(message,file,function, line)
}

public class func warning(_ message: String?, _ file: String = #file, _ function: String = #function, line: Int = #line) {
    sharedInstance.warning(message,file,function, line)
}

public class func error(_ message: String?, _ file: String = #file, _ function: String = #function, line: Int = #line) {
    sharedInstance.error(message,file,function, line)
}

//MARK: Private methods
private static let sharedInstance = T21Logger("APP")
}

此处用法

AppLogger.verbose("Verbose message!")
AppLogger.error("Error message!")
💭18:26:04.545 VERBOSE AppDelegate.application():22 - [APP] Verbose message!
🚫18:26:04.546 ERROR AppDelegate.application():26 - [APP] Error message!

添加DEBUG行为

当使用CocoaPods依赖管理器时,自动生成的项目没有为pod依赖项添加DEBUG标志。因为当使用DEBUG选项编译时,T21LoggerSwift的工作方式不同,你可以在Podfile中添加以下安装后的脚本。

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            if config.name == 'Debug'
                #in case of using Objective-C 
                config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'DEBUG=1']
                #in case of using Swift
                config.build_settings['OTHER_SWIFT_FLAGS'] ||= ['$(inherited)','-DDEBUG']
            end
        end
    end
end

编译方式

  • SwiftyBeaver - Swift开发与发布中便捷的日志记录。

贡献者

  • Eloi Guzman Ceron - 初始工作
  • Patricia De la Rica - Carthage集成
  • Marcos Molero - Carthage集成

许可证

本项目采用MIT许可证 - 详细内容请参阅LICENSE.md文件。

感谢

  • 向Worldline iOS Dev Team致谢。