DogSwift
轻量级且易于使用的日志记录 API,可用于您的移动 iOS 应用程序。
DogSwift 在 iOS 10.0+ 设备上使用 os_log
,对于较旧的 iOS 版本则回退到 NSLog
。DogSwift 可将错误、消息和对象描述打印到 Xcode 的调试控制台。还可以通过类别(如 UI
、Networking
等)标记每个日志语句,这些类别将被 Apple 的 os_log
实现 utilised。
说明
添加环境变量 LOG_LEVEL
以及您想要的值
error = 1
warn = 2
info = 3
debug = 4
记录到 Xcode 的调试控制台
// Log function name of current scope.
Log.info(#function)
// The message parameter can be of type `Any`.
Log.debug(view.bounds, tag: .ui)
// Log a warning.
Log.warn("Current device is running iOS 9.0, which doesn't support os_log.", tag: .location)
// Error logging.
let exampleError = NSError(domain: "ch.dreipol", code: -9999, userInfo: [
NSLocalizedDescriptionKey: "Flying to the moon was not possible.",
NSLocalizedFailureReasonErrorKey: "The operation timed out.",
NSLocalizedRecoverySuggestionErrorKey: "Have you tried turning it off and on again?"
])
Log.error(exampleError, description: #function, tag: .system
带有标记的日志记录
标签可以帮助对日志输出进行分组。目前支持以下标签
none
database
networking
system
ui
定义自定义标签
您可以定义自己的标签并将它们传递给DogSwift。
import DogSwift
enum ExampleTag: String {
case viewDidLoad
}
extension ExampleTag: TagProtocol {
func makeString() -> String {
return String(describing: self)
}
}
现在可以将新的Tag传递给DogSwift。
[...]
override func viewDidLoad() {
super.viewDidLoad()
Log.debug(view.bounds, tag: ExampleTag.viewDidLoad)
}
[...]
要求
Swift 5.0
使用 CocoaPods 安装
DogSwift 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中。
pod 'DogSwift'
DogSwiftExample
要运行示例项目,请打开 DogSwiftExample 目录中的 DogSwiftExample.xcworkspace
。示例应用程序包含一些关于如何使用 DogSwift 来记录错误以及发送各种信息到 Xcode 调试控制台的示例。