Skip to content

Instantly share code, notes, and snippets.

@saoudrizwan
Last active October 5, 2019 05:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saoudrizwan/83f995fa9e7b45c2c3184d1a1cd6eaf2 to your computer and use it in GitHub Desktop.
Save saoudrizwan/83f995fa9e7b45c2c3184d1a1cd6eaf2 to your computer and use it in GitHub Desktop.
Easy way to log file name, function, and line number to Console using Unified Logging in Swift. See String format specifiers: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
import os.log
os_log("[%{public}@/%{public}@:%{public}@] This is an error message", log: OSLog(subsystem: "my.system", category: "Networking"), type: OSLogType.error, ("\(#file)" as NSString).lastPathComponent, "\(#function)", "\(#line)")
// Alternatively use a global helper method
enum LogCategory: String {
case `default` = "Default"
case networking = "Networking"
}
func log(function: String = #function, file: String = #file, line: Int = #line, _ category: LogCategory = .default, _ type: OSLogType = .default, _ message: String) {
os_log("[%{public}@/%{public}@:%{public}@] %{public}@", log: OSLog(subsystem: "my.system", category: category.rawValue), type: type, (file as NSString).lastPathComponent, function, "\(line)", message)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment