SwiftDebugLog
简单 Swift 调试日志,灵感来源于 这篇 Medium 帖子
用途
-
全局封装 print 函数。除非您将代码编译在 Debug 模式下,否则所有打印语句都会在编译时删除。 在生产环境中进行调试打印可以使用:
Log.logProduction(error)
-
允许您轻松记錯,包括文件名、行号和函数名,而无需额外的类型。
示例输出
2019-02-17 15:29:25.627-0500
2019-02-17 15:29:25.633-0500
2019-02-17 15:29:25.633-0500 🕵 [ViewController.swift] 行: 43 makeApiCall() -> {"userId": 1, "id": 1, "title": "delectus aut autem"}
2019-02-17 15:29:25.634-0500
2019-02-17 15:29:25.634-0500
创建以上日志的代码
func logInProduction() {
// This should only be used when debugging in production!
Log.logProduction("This log always prints!")
}
func trySomething() {
do {
// Something that might throw an error
let _ = try String(contentsOfFile: "")
} catch let error {
Log.logError(error.localizedDescription)
}
}
func makeApiCall() {
let json: [String: Any] = [
"userId": 1,
"id": 1,
"title": "delectus aut autem"
]
Log.logDebug(json)
}
func doSomethingWith(optional: String?) {
guard let string = optional else {
Log.logWarning("Optional is nil!")
return
}
}
func doSomethingReallyImportant(shouldCrash: Bool) {
if shouldCrash {
Log.logSevere("Crashing for some reason")
fatalError()
}
}
日志级别
- 错误 =
😡 - 调试 = 🕵
- 警告 =
⚠️ - 严重 =
🚨 🚨 - 生产 =
🐛 🐛 🐛 🐛
示例
要运行示例项目,请先克隆仓库,然后在 Example 目录中运行 pod install
。
安装
SwiftDebugLog 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod 'SwiftDebugLog'
或者将 Log.swift 的内容复制并粘贴到您的项目中!使用此方法,您无需在文件中 import SwiftDebugLog
,并且它还会全局包装 print
以仅用于调试。
许可
SwiftDebugLog 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。