Last active
September 24, 2020 15:10
-
-
Save 2no/5d38b77def316d55084b02a387e2c73f to your computer and use it in GitHub Desktop.
swift-log+SwiftyBeaver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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