Skip to content

Instantly share code, notes, and snippets.

@koingdev
Created May 1, 2019 14:42
Show Gist options
  • Save koingdev/827d1143e1d0dfc8e3a13c19bfc8c7a6 to your computer and use it in GitHub Desktop.
Save koingdev/827d1143e1d0dfc8e3a13c19bfc8c7a6 to your computer and use it in GitHub Desktop.
Simple Swift Logger
func dPrint(_ item: Any) {
#if DEBUG
Swift.print(item)
#endif
}
final class Log {
private enum LogType: String {
case error = "[⛔️]"
case debug = "[ℹ️]"
case warning = "[⚠️]"
}
private static func sourceFileName(filePath: String) -> String {
let fileName = filePath.components(separatedBy: "/").last
return fileName ?? ""
}
static func error(_ object: Any,
fileName: String = #file,
line: Int = #line,
funcName: String = #function) {
dPrint("Log: \(LogType.error.rawValue) [\(sourceFileName(filePath: fileName))] [Line: \(line)] \(funcName) -> \(object)")
}
static func debug(_ object: Any,
fileName: String = #file,
line: Int = #line,
funcName: String = #function) {
dPrint("Log: \(LogType.debug.rawValue) [\(sourceFileName(filePath: fileName))] [Line: \(line)] \(funcName) -> \(object)")
}
static func warning(_ object: Any,
fileName: String = #file,
line: Int = #line,
funcName: String = #function) {
dPrint("Log: \(LogType.warning.rawValue) [\(sourceFileName(filePath: fileName))] [Line: \(line)] \(funcName) -> \(object)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment