DLLogging
一个支持抽象日志框架
- 统一日志。
- 模块化、集中化日志。
- 可以更容易地插件新日志。
- 完全自定义日志消息的格式。
- 内置日志记录
- 控制台日志
- PrintLogging:Swift的print。
- PrintDebugLogging:Swift的debugPrint。
- 文件日志
- 将日志消息写入文件。
- 它将内容以UTF-8编码的数据刷新,并回调客户端以处理每个配置好的时间间隔并清除内容。
- 控制台日志
支持的日志级别
🗣 详细:一个详细的消息,通常在处理特定问题时非常有用。🔍 调试:一个可能对开发人员有用的调试消息。ℹ️ 信息:一个信息消息,在粗粒度级别上突出显示应用程序的进度。⚠️ 警告:一个警告消息,可能表示可能出现的错误。❗️ 错误:发生错误,但可以恢复,只是关于发生了什么的说明。🛑 严重:服务器发生错误。
需求
- Xcode 11+
- Swift 5.0+
如何
设置
- 使用框架的默认设置。
import UIKit
import DLLogging
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
/// Setup Logging
LoggerManager.sharedInstance.initialize()
return true
}
}
- 使用支持的日志记录。
import UIKit
import DLLogging
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
/// Setup Logging
let logFormatter = LogFormatterImpl()
LoggerManager.sharedInstance.addLogging(LoggerFactoryImpl.makeConsoleLogging(logFormatter: logFormatter))
LoggerManager.sharedInstance.addLogging(LoggerFactoryImpl.makeFileLogging(fileName: "logs"))
/// Disable LogLevels. Enable all LogLevels by default
LoggerManager.sharedInstance.disableLogLevels([LogLevel.info, LogLevel.error])
return true
}
}
- 添加您的自定义日志记录。
import UIKit
import DLLogging
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
/// Setup Logging
let logFormatter = LogFormatterImpl()
let testLogging = TestLogging(logFormatter: logFormatter)
LoggerManager.sharedInstance.addLogging(testLogging)
return true
}
}
/// Your custom Logging.
final class TestLogging: BaseLogging {
let logger = OSLog.init(subsystem: "com.domain.loggingdemo", category: "main")
override func receiveMessage(_ message: LogMessage) {
if let formattedMessage = logFormatter?.formatMessage(message) {
os_log("%@", log: logger, type: OSLogType.debug, formattedMessage)
} else {
os_log("Your message %@", message.text)
}
}
}
- 添加您的自定义格式化程序。
import UIKit
import DLLogging
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
/// Setup Logging
let logFormatter = CustomLoggingFormatter()
let testLogging = LoggerFactoryImpl.makeConsoleLogging(logFormatter: logFormatter)
LoggerManager.sharedInstance.addLogging(testLogging)
return true
}
}
/// Your custom Formatter
final class CustomLoggingFormatter: LogFormatter {
func formatMessage(_ message: LogMessage) -> String {
return "[\(message.level.symbol)][\(message.function)] -> \(message.text)"
}
}
使用
/// Invoke
Log.info(message: "info")
Log.debug(message: "debug")
Log.verbose(message: "verbose")
Log.warning(message: "warning")
Log.error(message: "error")
Log.severe(message: "severe")
/// Output
2020-07-16T18:50:09.254+0700 [ℹ️][ViewController.swift:18:viewDidLoad()] -> info
2020-07-16T18:50:09.256+0700 [🔍][ViewController.swift:19:viewDidLoad()] -> debug
2020-07-16T18:50:09.256+0700 [🗣][ViewController.swift:20:viewDidLoad()] -> verbose
2020-07-16T18:50:09.257+0700 [⚠️][ViewController.swift:21:viewDidLoad()] -> warning
2020-07-16T18:50:09.257+0700 [❗️][ViewController.swift:22:viewDidLoad()] -> error
2020-07-16T18:50:09.257+0700 [🛑][ViewController.swift:23:viewDidLoad()] -> severe
安装
安装DLLogging
有三种方式
CocoaPods
只需将以下内容添加到项目的Podfile
文件中
pod 'DLLogging', '~> 1.2'
Carthage
将以下内容添加到Cartfile
github "lengocduy/DLLogging" ~> 1.2
Swift包管理器
创建一个Package.swift
文件
// swift-tools-version:5.0
import PackageDescription
let package = Package(
name: "TestLogging",
dependencies: [
.package(url: "https://github.com/lengocduy/DLLogging.git", from: "1.2.0"),
],
targets: [
.target(
name: "TestLogging",
dependencies: ["DLLogging"])
]
)
架构
交互流程
许可证
DLLogging 代码可用MIT许可证。有关更多信息,请参阅许可证文件。