Last active
October 5, 2019 05:11
-
-
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
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 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