Apple swift-log 的一个日志引擎后端
apple/swift-log
的后端[stdout/stderr, file, oslog, syslog]
//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")
终端输出
logger.logLevel = .info
logger.debug("will not log")
logger.info("will log")
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")])
终端输出
let fileSink = FileSink("path/of/log")
日志信息将写入文件。
更多信息请参考 Tests。
let osLogSink = OSLogSink(subsystem: "subsystem", category: "category")
日志信息将写入 OS log (Apple 的 syslog)。 在 macOS 中使用 Console.app
查看 oslog 信息。
更多信息请参考 Tests。
let systemLogSink = SystemLogSink()
日志信息将写入 syslog。
更多信息请参考 Tests。
Formation
用于生成日志信息。
内置了一些 Formation
:
Formation.standard
。 这是 stdout/stderr
的默认配置。Formation.standardXcode
。 这是在 Xcode 中显示 stdout/stderr
的默认配置,因为 Xcode 的控制台不支持 ANSI 转义码。 Formation.file
。 这是 File
的默认配置。Formation.os
。 这是 OSLog
的默认配置。Formation.system
。 这是 syslog
的默认配置。创建 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 文件。
Printable
增强了你的日志信息,它可以为组件添加颜色或样式。
内置的 Printer
有两个默认实例。 Printer.standard
用于 stdout/stderr
。 Printer.xcode
用于 Xcode 中的 stdout/stderr
。
你可以创建新的 Printer
实例或使用实现了 Printable
协议的 YourPrinter
。
Separator
字符串用于分隔日志信息的组件。 Formation.standard
的分隔符是 " ▶ "
。
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。
将以下内容添加到你的 Package.swift
文件中
dependencies: [
.package(url: "https://github.com/L1MeN9Yu/Senna.git", from: "3.0.0")
]
Senna 基于 MIT 许可证。 详细信息请参阅 LICENSE 文件。
感谢 JetBrains 提供的开源开发许可证。JetBrains 通过为项目核心开发者免费提供开发工具来支持非商业开源项目。
感谢 JetBrains 提供的开源开发许可证。JetBrains 通过为项目核心开发者免费提供开发工具来支持非商业开源项目。