Skip to content

Instantly share code, notes, and snippets.

@juliengdt
Last active May 19, 2016 09:28
Show Gist options
  • Save juliengdt/e71b03204d344d4a7d9855444763aacc to your computer and use it in GitHub Desktop.
Save juliengdt/e71b03204d344d4a7d9855444763aacc to your computer and use it in GitHub Desktop.
Swift Protocol Logger with level condition, Swift 2.X and Swift 3.X version
//: Playground - noun: a place where people can play
// Usage for Swift 2.X using the deadly old Swift Debugger Identifiers, ie: __FILE__
// @see: https://github.com/apple/swift-evolution/blob/master/proposals/0028-modernizing-debug-identifiers.md
enum LogLevel: Int {
case verbose = 1
case debug = 2
case info = 3
case warning = 4
case error = 5
}
func >=(lhs: LogLevel, rhs: LogLevel) -> Bool {
return lhs.rawValue >= rhs.rawValue
}
protocol Log {
/// Writes a log entry. Types that
/// conform to `Log` must implement
/// this to perform their work.
///
/// - Important: Clients of `Log`
/// should never call this method.
/// Always call `log(_:,_:)`.
func writeLogEntry(
logLevel: LogLevel,
@autoclosure _ message: () -> String,
file: StaticString,
line: Int,
function: StaticString)
}
extension Log {
/// The public API for `Log`. Calls
/// `writeLogEntry(_:,_:,file:,line:,function:)`.
func log(
logLevel: LogLevel,
@autoclosure _ message: () -> String,
file: StaticString = __FILE__,
line: Int = __LINE__,
function: StaticString = __FUNCTION__)
{
writeLogEntry(logLevel, message,
file: file, line: line,
function: function)
}
}
struct HCLog {
let minimumLogLevel: LogLevel
}
extension HCLog: Log {
func writeLogEntry(
logLevel: LogLevel,
@autoclosure _ message: () -> String,
file: StaticString,
line: Int,
function: StaticString)
{
if logLevel >= minimumLogLevel {
print("\(logLevel) – \(file):\(line) – \(function) – \(message())")
}
}
}
//: Playground - noun: a place where people can play
// Usage for Swift 3.X using the brand new Swift Debugger Identifiers, is: #file
// @see: https://github.com/apple/swift-evolution/blob/master/proposals/0028-modernizing-debug-identifiers.md
enum LogLevel: Int {
case verbose = 1
case debug = 2
case info = 3
case warning = 4
case error = 5
}
func >=(lhs: LogLevel, rhs: LogLevel) -> Bool {
return lhs.rawValue >= rhs.rawValue
}
protocol Log {
/// Writes a log entry. Types that
/// conform to `Log` must implement
/// this to perform their work.
///
/// - Important: Clients of `Log`
/// should never call this method.
/// Always call `log(_:,_:)`.
func writeLogEntry(
logLevel: LogLevel,
@autoclosure _ message: () -> String,
file: StaticString,
line: Int,
function: StaticString)
}
extension Log {
/// The public API for `Log`. Calls
/// `writeLogEntry(_:,_:,file:,line:,function:)`.
func log(
logLevel: LogLevel,
@autoclosure _ message: () -> String,
file: StaticString = #file,
line: Int = #line,
function: StaticString = #function)
{
writeLogEntry(logLevel, message,
file: file, line: line,
function: function)
}
}
struct HCLog {
let minimumLogLevel: LogLevel
}
extension HCLog: Log {
func writeLogEntry(
logLevel: LogLevel,
@autoclosure _ message: () -> String,
file: StaticString,
line: Int,
function: StaticString)
{
if logLevel >= minimumLogLevel {
print("\(logLevel) – \(file):\(line) – \(function) – \(message())")
}
}
}
let logger = HCLog(minimumLogLevel: .error)
logger.log(.verbose, "this is a verbose test") // NOPE ⛔️
logger.log(.error, "This is a error test") // 🎉
logger.log(.error, "This is another error test") // 🎉
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment