I'd like to give control to the app developer that uses my library to select which level of logs they'd like to see from my lib (e.g. do they want to see all debug messages or just errors).
I know there are filtering controls that Xcode gives us, but I'm wondering if there is a way to pull this off with code. Ideally the user callsite would look like MyLib(logLevel: .info)
.
And then I would pass that info
level somehow to OSLog. Today, I create my logger like this:
let myLogger = Logger(
subsystem: Bundle.main.bundleIdentifier ?? "UnknownApp",
category: "MyLibrary"
)
As far as I can tell, there is nothing I can then pass to my myLogger
instance to configure the threshold level. I'm imagining an interface like:
myLogger.logLevel(.warning)
// Later...
myLogger.debug("You won't see this")
myLogger.error("But you will see this")
Does OSLog and friends give us any ability to do this out of the box, or are we building little wrappers around OSLog to accomplish this?
Thank you, Lou
You can’t associate a level with a Logger
object, but it is possible to log at a user-supplied level:
import os.log
class MyLib {
init(logLevel: OSLogType) {
self.log = Logger(subsystem: "com.example.Test774931", category: "general")
self.logLevel = logLevel
}
let log: Logger
let logLevel: OSLogType
func someMethod() {
log.log(level: self.logLevel, "will start some method")
… do stuff …
log.log(level: self.logLevel, "did finish some method")
}
}
Is that sufficient?
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"