Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jordanebelanger/1a61e460276b5332204723f928b4155e to your computer and use it in GitHub Desktop.
Save jordanebelanger/1a61e460276b5332204723f928b4155e to your computer and use it in GitHub Desktop.
Async logging of error
import NIO
import Logging
extension EventLoopFuture {
func whenFailureLog(
level: Logger.Level = .error,
_ message: @escaping @autoclosure () -> Logger.Message,
using logger: Logger,
errorDescriptionMetadataKey: String = "reason",
metadata: @escaping @autoclosure () -> Logger.Metadata? = nil,
file: String = #file, function: String = #function, line: UInt = #line
) {
self.whenFailure { error in
var logger = logger
logger[metadataKey: errorDescriptionMetadataKey] = "\(error)"
logger.log(level: level, message(), metadata: metadata(), file: file, function: function, line: line)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment