Last active
May 19, 2016 09:28
-
-
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
This file contains hidden or 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
//: 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())") | |
} | |
} | |
} |
This file contains hidden or 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
//: 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())") | |
} | |
} | |
} |
This file contains hidden or 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
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