Hi and Merry Christmas to everybody that is celebrating these Holidays.
I had some time on my hands so I tried to tackle (one of) the most requested feature when wrapping the logger: specifying a minimum log level programmatically.
I don’t know if this is what you had seen other people try @eskimo, but maybe my attempt could be useful, mainly to avoid people doing something like this:
// Simplified for brevity
struct ConditionalLogger {
let minLogLevel: LogLevel
let logger = Logger()
func log(level: LogLevel, _ message: String) {
if minLogLevel < level {
logger.log("\(message, privacy: .public)")
}
}
}
If you guys want to take a look, you can see the macro I just created here: GitHub - Enricoza/EZLog: A macro that wraps the Swift Logger to limit logs based on log level, without impacting Logger's performance and capabilities.
Usage is pretty straightforward.
First you create an EZLogger instance (or multiple ones) with a minimum allowed log level:
let logger = EZLogger(subsystem: "com.app.my",
category: "SomeCategory",
logLevel: .error)
And then you use it in all of your loggings:
#debug(logger, "Some debug message with: \(someVariable, privacy: .private)")
#info(logger, "Some info message with: \(someVariable, privacy: .public)")
The macro then just basically expands in a ternary operator that compares minimum and current log level to determine if the log should happen. More about the implementation can be read on the repo.
It would be interesting to hear your thoughts on it if anybody has a chance to try it out.