Last active
September 7, 2017 14:44
-
-
Save kayoslab/96307f058e7ca55f6ca2a1f2e5de90ea to your computer and use it in GitHub Desktop.
Quick test case implementation of a logging manager
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
class Logging { | |
/// An Enum that describes the actual Level on which | |
/// Messages should be logged to the console. | |
public enum Level: Int { | |
/// Log all messages. | |
case verbose = 0 | |
/// Log messages of information level and higher. | |
case information = 1 | |
/// Log messages of warning level and higher. | |
case warning = 2 | |
/// Log messages of error level and higher. | |
case error = 3 | |
/// Log only critical messages. | |
case critical = 4 | |
internal var levelName: String { | |
switch self { | |
case .verbose: return "Verbose" | |
case .information: return "Information" | |
case .warning: return "Warning" | |
case .error: return "Error" | |
case .critical: return "Critical" | |
} | |
} | |
} | |
internal var loggingLevel: Level = .information | |
internal static let shared: Logging = Logging() | |
private init() {} | |
internal func write(message: String, with level: Level) { | |
if level.rawValue >= loggingLevel.rawValue { | |
print(message, with: level) | |
} | |
} | |
} | |
internal func print(_ items: Any..., with level: Logging.Level = .information) { | |
let dateFormatter: DateFormatter = DateFormatter() | |
dateFormatter.dateFormat = "HH:mm:ss" | |
let date = Date() | |
let dateString = dateFormatter.string(from: date) | |
Swift.print("\(level.levelName) [\(dateString)]: \(items)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment