日志记录器 (Logger)

Test

满足简单需求的简单日志记录。

入门指南

在你的 Package.swift 文件中声明依赖关系。

注意:此库不稳定,任何更新都可能引入 BREAKING CHANGE(重大更改)。

.package(name: "Logger", url: "https://github.com/binaryscraping/swift-log.git", from: "0.1.0"),

并将其添加到你的应用程序目标中。

.target(name: "BestExampleApp", dependencies: ["Logger"]),

现在,让我们学习如何使用它。

// Import the logger package.
import Logger

// Create a logger instance, the system is a string that identifies this logging instance.
// It's recommended to use an inverse domain like your bundle identifier.
// This logging implementation is based on destinations, each destination represent a place where
// the log messagens will be sent.
let logger = Logger(system: "co.binaryscraping.best-example-app", destinations: [.console(), .file(url: URL(...)])

// Then, just call the available methods on the logger instance, there is one method for each
// logging level. [verbose, debug, info, warning and error].
logger.info("Hello World!")

// There's support for adding additional context to a log message.
logger.error("something is not working", context: ["user_id": "deadbeef"])

如果你愿意,可以通过 Logger.main 定义一个共享的日志记录器实例。

Logger.main = Logger(system: "co.binaryscraping.best-example-app", destinations: [.console(), .file(url: URL(...)])

如果在初始化之前使用 Logger.main,应用程序将会崩溃。

目标 (Destinations)

这个包提供了三种开箱即用的目标,分别是 console(控制台)、file(文件)和 sqlite(SQLite)。

console 目标将日志消息发送到控制台,并且仅限于 debug(调试)模式,因此在 release(发布)构建中不会发送任何日志。

file 目标将日志消息写入用户必须提供的本地文件。

sqlite 目标将日志写入 SQLite 数据库,这是最强大的目标,因为你可以聚合和查询日志。

两种目标都接受一个 Formatter(格式化器)参数,以自定义消息的格式化逻辑,有一个默认的实现。

SQLite 目标

有一个 SQLite 目标的默认实现,它支持过滤日志消息。

// Initialize a new SQLiteLoggingStore passing the database path.
// It's important to keep a single instance of a SQLiteLoggingStore through the whole life cycle.
let store = try SQLiteLoggingStore(path: "path/to/db.sqlite")

// Init a Logger instance by passing the store's destination
let logger = Logger(
  system: "co.binaryscraping.logger.tests",
  destinations: [store.destination]
)

// Use the logger
logger.info("info message")

// And then query the log messages by using method `logs(where:)`.
let logs = try store.logs(
  where: .or(.level(.info), .level(.error)), .file("%Tests.swift"))
  
// The above filter is transformed into a SQL `where` clause and stands for:
// 'Fetch all log messages that has a level error or info and has happened on files that ends with "Tests.swift"'.
// Example: 'WHERE ("level" = 2 OR "level" = 4) AND ("file" LIKE "%Tests.swift")'

有关可用的过滤器,请参阅 Filter 枚举

自定义目标

这种目标方法使添加新目标非常容易,例如这个将日志消息发送到 Crashlytics 的目标示例。

import FirebaseCrashlytics

extension Logger.Destination {
  static let crashlytics = Logger.Destination { msg in
    // You can use the default formatter for generating a string, 
    // or implement your own formatting logic.
    let string = Logger.Formatter.default.format(msg)
    Crashlytics.crashlytics().log(string)
  }
}

// Then to use the new destination, simply init a logger passing the `crashlytics` destination.
Logger.main = Logger(system: "co.binaryscraping.best-example-app", destinations: [.console(), .crashlytics])

贡献

欢迎提交 Pull Request。

请确保适当地更新测试。

许可证

MIT