将 SwiftyBeaver 的强大日志功能添加到 Vapor 中,用于 Linux 和 Mac 上的服务器端 Swift。
将其添加到您的 Vapor 项目的 Package.swift
文件中
dependencies: [
.package(url: "https://github.com/SwiftyBeaver/SwiftyBeaver-Vapor.git", from: "1.1.0"),
//...other packages here
],
targets: [
.target(name: "App", dependencies: [
.product(name: "SwiftyBeaverVapor", package: "SwiftyBeaver-Vapor")
//...other packages here
])
]
在 configure.swift
中
import SwiftyBeaverVapor
import SwiftyBeaver
/// Called before your application initializes.
public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
// ...
// console logging
let consoleDestination = ConsoleDestination()
try services.register(SwiftyBeaverProvider(destinations: [consoleDestination]))
// OR console and cloud platform logging
let consoleDestination = ConsoleDestination()
let cloudDestination = SBPlatformDestination(appID: "xxx", appSecret: "xxxxxx", encryptionKey: "xxxxx")
try services.register(SwiftyBeaverProvider(destinations: [consoleDestination, cloudDestination]))
config.prefer(SwiftyBeaverVapor.self, for: Logger.self)
// ...
}
添加您想要使用的 SwiftyBeaver 日志目的地,可以选择调整它们的默认值,如格式、颜色、过滤器或最小日志级别,然后就可以开始记录日志了 🙌
例如,您可以记录传入的请求。
func index(_ req: Request) throws -> Future<[Foo]> {
let logger = try? req.sharedContainer.make(Logger.self)
logger?.log(req.description, at: .verbose, file: #file, function: #function, line: #line, column: #column)
return Foo.query(on: req).all()
}
有关更多信息,请参阅 SwiftyBeaver 目标文档 以及如何设置自定义日志格式。
了解更多关于彩色日志输出到 Xcode 8 控制台的信息。
了解更多关于日志输出到文件的信息,这对于 Terminal.app 爱好者或将日志存储在磁盘上非常有用。
了解更多关于发布期间将日志输出到 SwiftyBeaver 平台的信息!
通过 Github Issues、电子邮件和我们的公共 Slack 频道获取支持。
SwiftyBeaverVapor 在 MIT 许可证下发布。