Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jordanebelanger/4307bf34b4ff256c9c8ec52d94db905b to your computer and use it in GitHub Desktop.
Save jordanebelanger/4307bf34b4ff256c9c8ec52d94db905b to your computer and use it in GitHub Desktop.
Bootstrapping StackdriverLogging in a Vapor 4 application, the `LoggingSystem+StackdriverLogging.swift` file should go in your App target
import Vapor
import StackdriverLogging
extension LoggingSystem {
/// Prepare the `StackdriverLogHandlerFactory` and bootstrap the `LoggingSystem` to use `StackdriverLogHandler`s.
///
/// - Returns: A `StackdriverLoggerLifecycleHandler` that must be passed to the `Vapor.Application` `lifecyle` service
/// for proper shutdown of the NIO dependencies used by the logger when the `Application` shuts down.
public static func bootstrapStackdriverLogging(for environment: inout Environment,
with configuration: StackdriverLoggingConfiguration) throws -> StackdriverLoggerLifecycleHandler {
// Creating the NIO dependencies used by the Stackdriver Logger
let threadPool = NIOThreadPool(numberOfThreads: NonBlockingFileIO.defaultThreadPoolSize)
threadPool.start()
let fileIO = NonBlockingFileIO(threadPool: threadPool)
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: NonBlockingFileIO.defaultThreadPoolSize)
try StackdriverLogHandlerFactory.prepare(with: configuration,
fileIO: fileIO,
eventLoopGroup: eventLoopGroup)
LoggingSystem.bootstrap { [environment] label -> LogHandler in
if environment == .development {
var logger = MultiplexLogHandler([
ConsoleLogger(label: label, console: Terminal()),
StackdriverLogHandlerFactory.make()
])
logger.logLevel = configuration.logLevel
return logger
} else {
return StackdriverLogHandlerFactory.make()
}
}
return .init(threadPool: threadPool, eventLoopGroup: eventLoopGroup)
}
}
/// A Vapor `LifecycleHandler` that can be passed to the `Vapor.Application` for shutdowns of the NIO dependencies used
/// by the StackdriverLogHandlerFactory` at application shutdown.
public final class StackdriverLoggerLifecycleHandler: LifecycleHandler {
private let threadPool: NIOThreadPool
private let eventLoopGroup: EventLoopGroup
public init(threadPool: NIOThreadPool, eventLoopGroup: EventLoopGroup) {
self.threadPool = threadPool
self.eventLoopGroup = eventLoopGroup
}
public func shutdown(_ application: Application) {
try! self.threadPool.syncShutdownGracefully()
try! self.eventLoopGroup.syncShutdownGracefully()
}
}
import App
import Vapor
var env = try Environment.detect()
let loggerConfig = StackdriverLoggingConfiguration(logFilePath: "var/log/my-vapor-app.log", defaultLogLevel: .debug)
let loggerDependenciesLifecycleHandler = try LoggingSystem.bootstrapStackdriverLogging(for: &env,
with: loggerConfig)
let app = Application(env)
defer { app.shutdown() }
try configure(app)
// Passing in the `StackdriverLogging` lifecycle dependencies to cleanly shutdown the logging system dependencies.
app.lifecycle.use(loggerDependenciesLifecycleHandler)
try app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment