Senna

Apple swift-log 的一个日志引擎后端

状态

Swift5.0+ release Build codecov license

platforms

特性

示例

使用内置 Sink

简单的 stdout

//create sink
let sink = StandardSink.out()
//create formation
let formation = Formation.standard
//create log
var logger = Logger(label: "LogName") {
    Handler(name: $0, sink: sink, formation: formation, logLevel: .trace)
}
// do some logs
logger.trace("This is a trace message")
logger.debug("This is a debug message")
logger.info("This is a info message")
logger.notice("This is a notice message")
logger.warning("This is a warning message")
logger.error("This is a error message")
logger.critical("This is a critical message")

终端输出

修改 LogLevel

logger.logLevel = .info
logger.debug("will not log")
logger.info("will log")

添加 MetaData

logger[metadataKey: "UserID"] = .stringConvertible(9527)
logger.info("message with logger meta data")
logger.info("message with both logger and message meta data", metadata: ["UserName": .string("L1MeN9Yu")])

终端输出

内置 Sinks

FileSink

let fileSink = FileSink("path/of/log")

日志信息将写入文件。

更多信息请参考 Tests

OSLogSink (仅限 Apple 平台)

let osLogSink = OSLogSink(subsystem: "subsystem", category: "category")

日志信息将写入 OS log (Apple 的 syslog)。 在 macOS 中使用 Console.app 查看 oslog 信息。

更多信息请参考 Tests

SystemLogSink (Linux)

let systemLogSink = SystemLogSink()

日志信息将写入 syslog。

更多信息请参考 Tests

Formation

Formation 用于生成日志信息。

内置了一些 Formation:

  1. Formation.standard。 这是 stdout/stderr 的默认配置。
  2. Formation.standardXcode。 这是在 Xcode 中显示 stdout/stderr 的默认配置,因为 Xcode 的控制台不支持 ANSI 转义码。
  3. Formation.file。 这是 File 的默认配置。
  4. Formation.os。 这是 OSLog 的默认配置。
  5. Formation.system。 这是 syslog 的默认配置。

高级自定义 Formation

创建 Formation 很简单: Formation(components: <#T##[Component]##[Senna.Component]#>, printer: <#T##Printable?##Senna.Printable?#>, separator: <#T##String?##Swift.String?#>)

Formation 包含 components: [Component], printer: Printable?separator: String?

Component

Component 是你的日志信息的元素。 更多信息请参考 Component 文件。

Printable

Printable 增强了你的日志信息,它可以为组件添加颜色或样式。

内置的 Printer 有两个默认实例。 Printer.standard 用于 stdout/stderrPrinter.xcode 用于 Xcode 中的 stdout/stderr

你可以创建新的 Printer 实例或使用实现了 Printable 协议的 YourPrinter

更多信息请参考 PrinterPrintable

Separator

Separator 字符串用于分隔日志信息的组件。 Formation.standard 的分隔符是 " ▶ "

Apple 的 Unified Logging System

Senna 重新实现了 Apple 的 Unified Logging System 的字符串插值行为。

let sink = StandardSink.out()
let formation = Formation.standard
var logger = Logger(label: "LogName") {
    Handler(name: $0, sink: sink, formation: formation, logLevel: .trace)
}
logger[metadataKey: "UserID"] = .stringConvertible(9527)
#if DEBUG
let privacy = Privacy.public
#else
let privacy = Privacy.private
#endif
// default is private
logger.senna.notice("the user name is \("L1MeN9Yu")")
logger.senna.notice("the user name is \("L1MeN9Yu", privacy: privacy)")

终端输出

更多用法请参考 LoggerSennaTests.swift

安装

Swift Package Manager

将以下内容添加到你的 Package.swift 文件中

dependencies: [
    .package(url: "https://github.com/L1MeN9Yu/Senna.git", from: "3.0.0")
]

鸣谢

许可证

Senna 基于 MIT 许可证。 详细信息请参阅 LICENSE 文件。

Thanks | 鸣谢

感谢 JetBrains 提供的开源开发许可证。JetBrains 通过为项目核心开发者免费提供开发工具来支持非商业开源项目。

感谢 JetBrains 提供的开源开发许可证。JetBrains 通过为项目核心开发者免费提供开发工具来支持非商业开源项目。