LoggingKit 是一个基于日志提供器的微型日志框架。
Combine
OSLogProvider
,底层使用 os_log
示例应用程序是了解 LoggingKit
如何工作的最佳方式。只需打开 LoggingKit.xcodeproj
并运行 Example
scheme。
应用程序启动后,您应该在 Xcode 终端和运行该应用程序的设备的 Console.app
中看到一些日志消息。
要使用 Apple 的 Swift Package Manager 集成,请将以下内容作为依赖项添加到您的 Package.swift
中
dependencies: [
.package(url: "https://github.com/alexanderwe/LoggingKit.git", from: "2.0.0")
]
或者,导航到您的 Xcode 项目,选择 Swift Packages
并单击 +
图标以搜索 LoggingKit
。
如果您不喜欢使用任何上述依赖管理器,您可以手动将 LoggingKit 集成到您的项目中。 只需将 Sources
文件夹拖到您的 Xcode 项目中。
首先,在 LogCategories
上创建一个扩展来定义你自己的类别是很有意义的。
import LoggingKit
extension LogCategories {
public var viewControllers: LogCategory { return .init("viewControllers") }
public var networking: LogCategory { return .init("networking") }
...
}
然后在 application(application:didFinishLaunchingWithOptions:)
中注册您的日志提供器。
import LoggingKit
class AppDelegate: UIResponder, UIApplicationDelegate {
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
LogService.register(logProviders: LogProvider, LogProvider ...)
}
...
}
之后,只需在要使用日志方法的文件中导入 LoggingKit
,然后相应地使用它们
import LoggingKit
LogService.shared.debug("Hello Debug", logCategory: \.viewControllers)
LogService.shared.verbose("Hello Verbose", logCategory: \.viewControllers)
LogService.shared.info("Hello Info", logCategory: \.viewControllers)
LogService.shared.warning("Hello Warning", logCategory: \.viewControllers)
LogService.shared.error("Hello Error", logCategory: \.viewControllers)
如果您正在使用 Combine,LoggingKit
在 Publisher
类型上提供了一些扩展,用于记录 Self.Output
和 Self.Failure
。
您可以选择您想要的任何类别。 \.combine
类别是一个自定义定义的类别。
import LoggingKit
// logs `Self.Output`
myPublisher.logValue(logType: .info, logCategory: \.combine) {
"My Value is \($0)"
}
// logs `Self.Failure`
myPublisher.logError(logCategory: \.combine) {
"My Error is \($0)"
}
// logs `Self.Output` as well as `Self.Failure`
myPublisher.log()
这个小框架背后的想法是,你可以通过编写你自己的日志提供器并遵循 LogProvider
协议来扩展它。 然后,这些实现可以在 LogService.register(providers:)
方法中注册。
您可以在 ./Example/MyTestLogProvider.swift 中找到一个示例 LogProvider
实现。
LoggingKit 自带一个预定义的 OSLogProvider
。 它在底层使用 os_log
来记录您的消息。 然后可以在您 mac 的 Console.app
应用程序和 Xcode 的控制台中查看这些消息。
在您的 mac 上打开 Console.App
,选择您要查看日志消息的设备,以查看由 OSLogProvider
打印的消息。
非常欢迎大家贡献代码 🙌
LoggingKit
Copyright (c) 2020 Alexander Weiß
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.