This is what I am using to wrap os_log:
import Foundation
import os.log
protocol LogServicing: class {
func debug(_ message: StaticString, _ args: CVarArg...)
func info(_ message: StaticString, _ args: CVarArg...)
func error(_ message: StaticString, _ args: CVarArg...)
}
enum LogType {
case debug
case info
case error
case fault
}
class LogService: LogServicing {
private var osLog: OSLog?
let subsystem: String
let category: String
init(subsystem: String = Bundle.main.bundleIdentifier ?? "", category: String = "") {
if #available(iOS 10.0, *) {
let osLog = OSLog(subsystem: subsystem, category: category)
self.osLog = osLog
}
self.subsystem = subsystem
self.category = category
}
func log(type: LogType, message: StaticString) {
log(type: type, message: message, "")
}
func log(type: LogType, message: StaticString, _ args: CVarArg...) {
if #available(iOS 10.0, *) {
guard let osLog = osLog else { return }
let logType: OSLogType
switch type {
case .debug:
logType = .debug
case .error:
logType = .error
case .fault:
logType = .fault
case .info:
logType = .info
}
os_log(message, log: osLog, type: logType, args)
print(message, args)
} else {
NSLog(message.description, args)
}
}
func debug(_ message: StaticString, _ args: CVarArg...) {
log(type: .debug, message: message, args)
}
func info(_ message: StaticString, _ args: CVarArg...) {
log(type: .info, message: message, args)
}
func error(_ message: StaticString, _ args: CVarArg...) {
log(type: .error, message: message, args)
}
}
And I created it like this:
self.logService = LogService(subsystem: "com.softbolt.app", category: "network")
And use it like this:
self.logService.info("HttpResponse %{public}@", url)
If you want to know more about os_log and the benefits of private and public logging check this link:
https://www.testdevlab.com/blog/2018/04/how-to-create-categorize-and-filter-ios-logs/