Unconventional logger
This file contains 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 | |
public struct Log { | |
fileprivate enum Tag: String { | |
case verbose = "🔷" | |
case success = "✅" | |
case warning = "⚠️" | |
case error = "🚫" | |
case json = "📦" | |
} | |
/// Verbose console print | |
/// | |
/// - Parameters: | |
/// - items: Items to print in the console | |
public static func v(_ item: Any, _ file: String = #file, _ line: Int = #line, _ function: String = #function) { | |
Log.consolePrint(.verbose, item: item, file: file, line: line, function: function) | |
} | |
/// Success console print | |
/// | |
/// - Parameters: | |
/// - items: Items to print in the console | |
public static func s(_ item: Any, _ file: String = #file, _ line: Int = #line, _ function: String = #function) { | |
Log.consolePrint(.success, item: item, file: file, line: line, function: function) | |
} | |
/// JSON console print | |
/// | |
/// - Parameters: | |
/// - items: JSON to print in the console | |
public static func j(_ json: Any, _ file: String = #file, _ line: Int = #line, _ function: String = #function) { | |
Log.consoleJSONPrint(json: json, file: file, line: line, function: function) | |
} | |
/// Warning console print | |
/// | |
/// - Parameters: | |
/// - items: Items to print in the console | |
public static func w(_ item: Any, _ file: String = #file, _ line: Int = #line, _ function: String = #function) { | |
Log.consolePrint(.warning, item: item, file: file, line: line, function: function) | |
} | |
/// Error console print | |
/// | |
/// - Parameters: | |
/// - items: Items to print in the console | |
public static func e(_ item: Any, _ file: String = #file, _ line: Int = #line, _ function: String = #function) { | |
Log.consolePrint(.error, item: item, file: file, line: line, function: function) | |
} | |
/// Private method that print the logs in the console | |
/// | |
/// - Parameters: | |
/// - tag: Tag Emoji to start the line | |
/// - items: Items to print in the console | |
/// - file: File from where the log has been written (do not overwrite) | |
/// - line: Line from where the log has been written (do not overwrite) | |
/// - function: Function from where the log had been written (do not overwrite) | |
fileprivate static func consolePrint(_ tag: Tag, item: Any, file: String, line: Int, function: String) { | |
#if DEBUG | |
let file = file.components(separatedBy: "/").last! | |
print("\(tag.rawValue) \(file.uppercased()) (\(line)) \(function): \(item)") | |
#endif | |
} | |
/// Private method that print JSON in the console | |
/// | |
/// - Parameters: | |
/// - jsons: JSONS to print in the console | |
/// - file: File from where the log has been written (do not overwrite) | |
/// - line: Line from where the log has been written (do not overwrite) | |
/// - function: Function from where the log had been written (do not overwrite) | |
fileprivate static func consoleJSONPrint(json: Any, file: String, line: Int, function: String) { | |
#if DEBUG | |
let file = file.components(separatedBy: "/").last! | |
NSLog("(\(file.uppercased()):\(line)) - \(function) \(Tag.json.rawValue): \(json)") | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment