Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Last active September 2, 2016 17:09
Show Gist options
  • Save JadenGeller/3a573c37e69b51e2296477db319c9c83 to your computer and use it in GitHub Desktop.
Save JadenGeller/3a573c37e69b51e2296477db319c9c83 to your computer and use it in GitHub Desktop.
Lightweight Multi-Severity Named Logging
import Foundation
public final class Logger {
public let name: String?
private var minimumSeverity: Severity?
public func error(@autoclosure message: () -> String) {
guard minimumSeverity >= .error else { return }
NSLog(message())
}
public func warning(@autoclosure message: () -> String) {
guard minimumSeverity >= .warning else { return }
NSLog(message())
}
public func info(@autoclosure message: () -> String) {
guard minimumSeverity >= .info else { return }
NSLog(message())
}
public func debug(@autoclosure message: () -> String) {
guard minimumSeverity >= .debug else { return }
NSLog(message())
}
public func verbose(@autoclosure message: () -> String) {
guard minimumSeverity >= .verbose else { return }
NSLog(message())
}
private init(named name: String?) {
self.name = name
}
}
extension Logger {
public enum Severity: Int, Comparable {
case error
case warning
case info
case debug
case verbose
}
}
public func ==(lhs: Logger.Severity, rhs: Logger.Severity) -> Bool {
return lhs.rawValue == rhs.rawValue
}
public func <(lhs: Logger.Severity, rhs: Logger.Severity) -> Bool {
return lhs.rawValue < rhs.rawValue
}
extension Logger {
public typealias Channel = String -> ()
private static let ignore: Channel = { _ in }
private static let output: Channel = { message in NSLog(message) }
}
extension Logger {
public func disable() {
self.minimumSeverity = nil
}
public func enable(minimumSeverity severity: Severity) {
self.minimumSeverity = severity
}
}
public let log = Logger(named: nil)
extension Logger {
public private(set) static var loggers: [String : Logger] = [:]
public static func category(name: String) -> Logger {
if let logger = loggers[name] {
return logger
} else {
let logger = Logger(named: name)
loggers[name] = logger
if let severity = defaultMinimumSeverity {
logger.enable(minimumSeverity: severity)
}
return logger
}
}
public static var defaultMinimumSeverity: Severity?
public static func enable(minimumSeverity severity: Severity) {
defaultMinimumSeverity = severity
log.enable(minimumSeverity: severity)
loggers.values.forEach { $0.enable(minimumSeverity: severity) }
}
public static func disable() {
defaultMinimumSeverity = nil
log.disable()
loggers.values.forEach { $0.disable() }
}
}
extension Logger {
public func post(messageForSeverity: (minimumSeverity: Severity) -> String?) {
guard let severity = minimumSeverity else { return }
guard let message = messageForSeverity(minimumSeverity: severity) else { return }
NSLog(message)
}
}
// Setup logging
Logger.enable(minimumSeverity: .info)
Logger.category("special").enable(minimumSeverity: .error)
// Do some normal logging...
log.error("normal error")
log.info("normal info")
do {
let log = Logger.category("special")
// Do some special logging...
log.error("special error")
log.info("special info")
}
// Expected output:
// -> normal error -> normal info // -> special error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment