LogKit 是一个日志框架,旨在简化使用 Apple 提供的 os.log
API 的工作。
LogKit 支持 SwiftPM。您可以使用 SwiftPM 通过 Xcode 直接集成 LogKit,或者手动使用 Package.swift 集成。
要向您的 Xcode 项目添加软件包依赖项,请选择“File” > “Swift Packages” > “Add Package Dependency”并输入其仓库 URL。您也可以导航到您目标的“General”面板,并在“Frameworks, Libraries, and Embedded Content”部分,点击“+”按钮。在“Choose frameworks and libraries to add”对话框中,选择“Add Other”,然后选择“Add Package Dependency”。
除了添加仓库 URL 之外,您还可以在 GitHub 或 GitHub Enterprise 上搜索软件包。在 Xcode 的偏好设置中添加您的 GitHub 或 GitHub Enterprise 帐户,输入时将显示软件包仓库列表。以下屏幕截图显示了搜索词 ExamplePackage 的仓库列表。
创建一个 Package.swift
文件。
// swift-tools-version:6.0
import PackageDescription
let package = Package(
name: "LogKitExample",
dependencies: [
.package(url: "https://github.com/marekpridal/LogKit", from: "3.0.0")
],
targets: [
.target(name: "LogKitExample", dependencies: ["LogKit"])
]
)
import LogKit
// Configure what should be logged during session
Log.enabledLogging = [.default, .error, .expiration, .function, .inAppPurchase, .networking]
Log.deinit(of: self)
// 2020-05-10 15:34:17.452379+0200 xctest[10906:217188] [deinit] Deinit of -[LogKitTests testLogDeinit]
Log.default("Hello world")
// 2020-05-10 15:34:17.451431+0200 xctest[10906:217188] [default] Hello world
Log.error(NSError(domain: "logkit.tests", code: 0, userInfo: nil))
// 2020-05-10 15:34:17.454305+0200 xctest[10906:217188] [error] Unable to complete (logkit.tests error 0.)
Log.function(#function, in: #file)
// 2020-05-10 15:34:17.455434+0200 xctest[10906:217188] [function] testLogFunctionIn() LogKitTests/LogKitTests.swift
Log.requestCalled(function: #function)
// 2020-05-10 15:34:17.465002+0200 xctest[10906:217188] [networking] testLogRequestCalled() already called
Log.expiration(date: expiresDate)
// 2020-05-10 15:34:17.454920+0200 xctest[10906:217188] [expiration] [GMT] Valid until 2020-05-10 13:34:17 +0000
Log.request(URLRequest(url: URL(string: "https://github.com/marekpridal/LogKit")!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 30))
// 2020-05-10 15:34:17.458338+0200 xctest[10906:217188] [networking]
// ---REQUEST------------------
// URL -> https://github.com/marekpridal/LogKit
// METHOD -> GET
// HEADERS: {
// }
// ----------------------------
Log.response(response, data: data)
// ---RESPONSE------------------
// URL -> https://github.com/marekpridal/LogKit
// MIMEType -> application/octet-stream
// Status code -> -1
// HEADERS: {
// }
// Response data -> {"bar":"Hello world"}
// ----------------------------
Log.function(#function, text: "Log function")
// 2020-05-10 15:34:17.455465+0200 xctest[10906:217188] [function] testLogFunctionIn() Log function
Log.inAppPurchase("Purchasing...")
// 2020-05-10 15:34:17.455969+0200 xctest[10906:217188] [inAppPurchase] Purchasing...
Log.products(request: request)
// 2020-05-10 15:41:25.661150+0200 xctest[11170:222169] [inAppPurchase]
// ---REQUEST------------------
// <SKProductsRequest: 0x100bb6670>
// ----------------------------
Log.products(response: response)
// 2020-05-10 15:41:48.995140+0200 xctest[11187:222918] [inAppPurchase]
// ---RESPONSE------------------
// Invalid product identifiers []
// ----------------------------
// Products []
// ----------------------------
Log.products(request: request)
// 2020-05-10 15:42:30.262523+0200 xctest[11204:223751] [inAppPurchase]
// ---REQUEST------------------
// <SKProductsRequest: 0x100b09130>
// ----------------------------
Log.paymentQueue(queue)
// 2020-05-10 15:42:58.838761+0200 xctest[11254:225150] [inAppPurchase]
// ---QUEUE------------------
// <SKPaymentQueue: 0x100f07ee0>
// ----------------------------
Log.payment(transactions: transactions)
// 2020-05-10 15:43:17.628385+0200 xctest[11281:225904] [inAppPurchase]
// ---UPDATED TRANSACTIONS------------------
// [<SKPaymentTransaction: 0x1051040a0>]
// ----------------------------