Skip to content

Instantly share code, notes, and snippets.

@2no
Last active September 24, 2020 15:10
Show Gist options
  • Save 2no/5d38b77def316d55084b02a387e2c73f to your computer and use it in GitHub Desktop.
Save 2no/5d38b77def316d55084b02a387e2c73f to your computer and use it in GitHub Desktop.
swift-log+SwiftyBeaver
var logger = Logger(label: Bundle.main.bundleIdentifier!)
SwiftyBeaver.self.addDestination(ConsoleDestination())
SwiftyBeaver.self.addDestination(FileDestination())
LoggingSystem.bootstrap(SwiftyBeaverLogHandler.init)
#if DEBUG
logger.logLevel = .debug
#endif
logger.debug("test")
import Logging
import SwiftyBeaver
struct SwiftyBeaverLogHandler: LogHandler {
var metadata: Logger.Metadata = .init()
var logLevel: Logger.Level = .info
init(_: String) {
}
func log(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, file: String, function: String, line: UInt) {
SwiftyBeaver.self.custom(level: level.asSwiftyBeaverLevel,
message: message,
file: file,
function: function,
line: Int(line),
context: metadata)
}
subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
get {
metadata[metadataKey]
}
set {
metadata[metadataKey] = newValue
}
}
}
private extension Logger.Level {
var asSwiftyBeaverLevel: SwiftyBeaver.Level {
switch self {
case .trace:
return .verbose
case .debug:
return .debug
case .info:
return .info
case .notice:
return .warning
case .warning:
return .warning
case .error:
return .error
case .critical:
return .error
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment