Skip to content

Instantly share code, notes, and snippets.

@StevenMasini
Last active December 3, 2017 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StevenMasini/3245ffbf7f23eebb6fe82d24afd1455f to your computer and use it in GitHub Desktop.
Save StevenMasini/3245ffbf7f23eebb6fe82d24afd1455f to your computer and use it in GitHub Desktop.
Unconventional logger
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