Last active
February 4, 2020 21:26
-
-
Save danielsanfr/4460829c76c99fd0c2fc3528018e2e91 to your computer and use it in GitHub Desktop.
A simple struct for printing logs in a more "Android" way. Print time, file, line and function name, plus desired message.
This file contains hidden or 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
/** | |
* Log.swift | |
* Copyright (c) 2020 Daniel San <[email protected]> | |
* Created by Daniel San on 17/01/2020. | |
*/ | |
import Foundation | |
struct Log { | |
private init() { /* No instance */ } | |
public static func a(_ message: String, file: String = #file, function: String = #function, line: Int = #line, column: Int = #column) { | |
printLog(message: message, logLevel: "π A", file: file, function: function, line: line, column: column) | |
} | |
public static func e(_ message: String, file: String = #file, function: String = #function, line: Int = #line, column: Int = #column) { | |
printLog(message: message, logLevel: "β€οΈ E", file: file, function: function, line: line, column: column) | |
} | |
public static func w(_ message: String, file: String = #file, function: String = #function, line: Int = #line, column: Int = #column) { | |
printLog(message: message, logLevel: "π W", file: file, function: function, line: line, column: column) | |
} | |
public static func i(_ message: String, file: String = #file, function: String = #function, line: Int = #line, column: Int = #column) { | |
printLog(message: message, logLevel: "π I", file: file, function: function, line: line, column: column) | |
} | |
public static func d(_ message: String, file: String = #file, function: String = #function, line: Int = #line, column: Int = #column) { | |
printLog(message: message, logLevel: "π D", file: file, function: function, line: line, column: column) | |
} | |
public static func v(_ message: String, file: String = #file, function: String = #function, line: Int = #line, column: Int = #column) { | |
printLog(message: message, logLevel: "π V", file: file, function: function, line: line, column: column) | |
} | |
public static func wtf(_ message: String, file: String = #file, function: String = #function, line: Int = #line, column: Int = #column) { | |
printLog(message: message, logLevel: "π WTF", file: file, function: function, line: line, column: column) | |
} | |
private static func printLog(message: String, logLevel: String, file: String, function: String, line: Int, column: Int) { | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS" | |
print("\(dateFormatter.string(from: Date()))/\(logLevel)/\(formatFile(file)):\(line)#\(function): \(message)") | |
} | |
private static func formatFile(_ file: String) -> String { | |
let startIndex = file.index(after: file.lastIndex(of: "/")!) | |
let endIndex = file.lastIndex(of: ".")! | |
return String(file[startIndex..<endIndex]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment