Skip to content

Instantly share code, notes, and snippets.

@kayoslab
Last active September 7, 2017 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kayoslab/96307f058e7ca55f6ca2a1f2e5de90ea to your computer and use it in GitHub Desktop.
Save kayoslab/96307f058e7ca55f6ca2a1f2e5de90ea to your computer and use it in GitHub Desktop.
Quick test case implementation of a logging manager
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