Skip to content

Instantly share code, notes, and snippets.

@giftbott
Last active April 24, 2017 02:57
Show Gist options
  • Select an option

  • Save giftbott/8a3936240f21bbfc3e64991c94f4437f to your computer and use it in GitHub Desktop.

Select an option

Save giftbott/8a3936240f21bbfc3e64991c94f4437f to your computer and use it in GitHub Desktop.
Swift 로깅용
import Foundation
fileprivate enum LogType: String {
case info = "Info"
case error = "Error"
case debug = "Debug"
case checking = "Check"
case warning = "Warn"
case fatal = "Fatal"
}
/// Info Log
internal func iLog(_ objects: Any..., filename: String = #file, line: Int = #line, funcname: String = #function) {
baseLog("✳️", .info, filename, line, funcname, objects)
}
/// Error Log
internal func eLog(_ objects: Any..., filename: String = #file, line: Int = #line, funcname: String = #function) {
baseLog("❌", .error, filename, line, funcname, objects)
}
/// Debug Log
internal func dLog(_ objects: Any..., filename: String = #file, line: Int = #line, funcname: String = #function) {
baseLog("🔥", .debug, filename, line, funcname, objects)
}
/// Checking Log
internal func cLog(_ filename: String = #file, line: Int = #line, funcname: String = #function) {
baseLog("☑️", .checking, filename, line, funcname, ["동작 체크용 -----"])
}
/// Warning Log
internal func wLog(_ filename: String = #file, _ line: Int = #line, _ funcname: String = #function) {
baseLog("⚠️", .warning, filename, line, funcname, ["주의 ####"])
}
/// Fatal Log
internal func fLog(_ filename: String = #file, _ line: Int = #line, _ funcname: String = #function) {
baseLog("🆘", .fatal, filename, line, funcname, ["경고!!!!!!"])
}
fileprivate func baseLog(_ emoji: String, _ logType: LogType, _ filename: String, _ line: Int, _ funcname: String, _ objects: Array<Any>) {
#if DEBUG
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm:ss:SSS"
let timestamp = dateFormatter.string(from: Date())
let file = URL(string: filename)?.lastPathComponent.components(separatedBy: ".").first ?? "Unknown"
let queue = Thread.isMainThread ? "UI" : "BG"
print("️\(emoji)\(logType.rawValue) ⏱\(timestamp) 📂\(file) (\(queue)) ⚙️\(funcname) (\(line)) \(emoji)\(emoji)", terminator: " ")
let _ = objects.map { print($0, terminator: " ") }
print()
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment