Skip to content

Instantly share code, notes, and snippets.

@SuperShinyEyes
Last active September 5, 2017 14:00
Show Gist options
  • Save SuperShinyEyes/8eee6ff60d7e7d3bd126e763bd7a130e to your computer and use it in GitHub Desktop.
Save SuperShinyEyes/8eee6ff60d7e7d3bd126e763bd7a130e to your computer and use it in GitHub Desktop.
swift Logger
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