Last active
September 5, 2017 14:00
-
-
Save SuperShinyEyes/8eee6ff60d7e7d3bd126e763bd7a130e to your computer and use it in GitHub Desktop.
swift Logger
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
import Foundation | |
enum LogEvent: String { | |
case error = "[‼️]" | |
case info = "[ℹ️]" | |
case debug = "[💬]" | |
case verbose = "[🔬]" | |
case warning = "[⚠️]" | |
case severe = "[🔥]" | |
} | |
struct Logger { | |
static private var dateFormat = "yyyy-MM-dd hh:mm:ssSSS" | |
static fileprivate var dateFormatter: DateFormatter { | |
let formatter = DateFormatter() | |
formatter.dateFormat = dateFormat | |
formatter.locale = Locale.current | |
formatter.timeZone = TimeZone.current | |
return formatter | |
} | |
static private func sourceFileName(filePath: String) -> String { | |
let components = filePath.components(separatedBy: "/") | |
return components.isEmpty ? "" : components.last! | |
} | |
static func log(message: String, | |
event: LogEvent, | |
fileName: String = #file, | |
line:Int = #line, | |
funcName: String = #function) { | |
#if DEBUG | |
print("\(Date().toString()) \(event.rawValue)[\(sourceFileName(filePath: fileName))]:\(line) \(funcName) -> \(message)") | |
#endif | |
} | |
} | |
extension Date { | |
func toString() -> String { | |
return Logger.dateFormatter.string(from: self as Date) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment