SquidLogger
SquidLogger 是一个用 Swift 编写的多播日志记录器,可以将日志发送到多个日志中。
NetworkLogger.debug("hello world.")
PaymentLogger.info("hello world.")
输出
📋 debug [Network] 2018-03-24 03:38:02.494 AppDelegate.swift(18) application(_:didFinishLaunchingWithOptions:) - hello world.
💡 info [Payment] 2018-03-24 03:38:02.494 AppDelegate.swift(18) application(_:didFinishLaunchingWithOptions:) - hello world.
我为什么要做这个?
我的项目中包含多个日志。例如:Crashlytics 日志、Xcode 控制台、我们的日志服务器。
我不想写重复的代码。
let text = "Fail to fetching tha User data."
Crashlytics.log(text)
print(text)
OurLogServer.error(text)
我想按模块控制日志。
- 将日志记录到 Networking 是警告级别以上。
- 将日志记录到 Payment 是调试级别以上。
因此我需要这个日志记录器。
如何使用
基本上很简单
初始化和默认日志记录器。如果初始化时 SquidLoggerManager.configure() 参数为空,则日志流仅是默认控制台日志流。
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Initialize code
SquidLoggerManager.configure()
// Each logs with log level
IkaLogger.debug("hello world.")
IkaLogger.info("hello world.")
IkaLogger.warn("hello world.")
IkaLogger.error("hello world.")
IkaLogger.fatal("hello world.")
return true
}
}
输出
📋 info [Default] 2018-03-24 03:38:02.494 AppDelegate.swift(18) application(_:didFinishLaunchingWithOptions:) - hello world.
💡 info [Default] 2018-03-24 03:38:02.494 AppDelegate.swift(19) application(_:didFinishLaunchingWithOptions:) - hello world.
⚠️ warn [Default] 2018-03-24 03:38:02.496 AppDelegate.swift(20) application(_:didFinishLaunchingWithOptions:) - hello world.
🚫 error [Default] 2018-03-24 03:38:02.496 AppDelegate.swift(21) application(_:didFinishLaunchingWithOptions:) - hello world.
💔 fatal [Default] 2018-03-24 03:38:02.496 AppDelegate.swift(22) application(_:didFinishLaunchingWithOptions:) - hello world.
注意 这是一个示例,因此我不建议您这样做。因为这个库是一个多播记录器,不是一个日志记录器。所以请确认以下内容。
如何使用详细说明
更改日志级别
SquidLoggerManager.defaultLogLevel
将应用到所有日志流。
SquidLoggerManager.configureLogLevel(to: .info)
SquidLoggerManager.defaultLogLevel = .warn
添加新的日志流
首先,实现一个日志流。
struct ConsoleLogger: SquidLogStreaming {
public func print<T>(_ object: T) {
Swift.print(object)
}
}
接下来,将流添加到 SquidLoggerManager。
SquidLoggerManager.configure(streams: [ConsoleLogger()])
注意 如果添加任何新的日志流,则会从管理日志流中删除默认日志流。
更改日志流的日志级别
一个日志流(ConsoleLogger
)已经被实现。
SquidLoggerManager.configureLogLevel(to: .error, at: ConsoleLogger.self)
添加新的日志模块
如果您想用于网络区域,只需实现以下内容。
struct NetworkLogger: SquidLogger {
static var category: String = "Network"
static var logLevel: SquidLogLevel?
}
您只需调用一个 NetworkLogger。
NetworkLogger.info("User data fetch completed")
注意 SquidLogger 不是线程安全的。因为这个库的主要目的是多播,日志记录不是主要目的。
更改日志模块的日志级别
struct NetworkLogger: SquidLogger {
static var category: String = "Network"
static var logLevel: SquidLogLevel? = .debug
}
NetworkLogger.logLevel = .error
修改日志级别的符号
首先,实现符号器。
struct MyLogLevelSymbol: SquidLogLevelSymbol {
func toSymbol(from level: SquidLogLevel) -> String {
switch level {
case .info: return "I"
case .debug: return "D"
case .warn: return "W"
case .error: return "E"
case .fatal: return "F"
case .none: return ""
}
}
}
配置您的符号器实例。
SquidLoggerManager.logLevelSymboler = MyLogLevelSymbol()
修改日志消息格式
struct MyTextFormatter: SquidLogTextFormatter {
func format<T>(_ params: SquidLogParameter<T>) -> String {
return "\(params.object)"
}
}
SquidLoggerManager.textFormatter = MyTextFormatter()
修改日志流的日志消息格式
实现 SquidLogStreaming.makeLogText
方法。
struct ConsoleLogger: SquidLogStreaming {
func makeLogText<T>(_ params: SquidLogParameter<T>) -> String? {
return "\(params.level):\(params.object)"
}
}
过滤日志流的日志消息
实现 SquidLogStreaming.filterLogText
方法。
struct ConsoleLogger: SquidLogStreaming {
func filterLogText(_ text: String) -> String {
guard let regex = try? NSRegularExpression(pattern: "hello", options: []) else { return text }
let range = NSRange(location: 0, length: text.count)
return regex.stringByReplacingMatches(in: text, options: [], range: range, withTemplate: "<FILTERED>")
}
}
IkaLogger.warn("hello world.")
📋 warn [Default] 2018-03-24 03:38:02.494 AppDelegate.swift(18) application(_:didFinishLaunchingWithOptions:) - <FILTERED> world.
关于日志级别过滤
最终级别 | SquidLogManager。 defaultLogLevel |
SquidLogger。 logLevel |
SquidLogStreaming。 logLevel |
---|---|---|---|
debug | debug | nil | nil |
info | debug | info | nil |
warn | debug | nil | warn |
warn | debug | info | warn |
error | error | nil | nil |
info | error | info | nil |
warn | error | nil | warn |
warn | error | info | warn |
运行时要求
- Swift 3.2 或更高版本
- iOS 9 或更高版本
- Xcode 9.2 或更高版本
库实现策略
- 别忘了主要目的。
- 轻量级