Enhancement: Logger that can be flushed? to better cooperate with CocoaLumberjack
mman opened this issue · 1 comments
I use Log
and Logger
API for general logging needs in my custom packages on Linux and iOS. Everything works great.
On iOS I initialize the Log.logger
(for historical reasons and cooperation with legacy code) with my custom implementation of https://github.com/CocoaLumberjack/CocoaLumberjack based Logger
implementation.
Also works great. Except for one thing. CocoaLumberjack is high perf and highly async, and when my app crashes controllably, the last few most important log statements are lost.
For that CocoaLumberjack provides a method DDLog.flushLog
https://github.com/CocoaLumberjack/CocoaLumberjack/blob/832adf8139ee8b9810683a110a2318a9ea225a5a/Sources/CocoaLumberjack/include/CocoaLumberjack/DDLog.h#L367.
I would like to open a PR to propose addition of Logger.flush()
and Log.flush()
methods that would allow my app to invoke these methods and propagate them down in order to make sure logging is flushed in cases where app is terminated in a controlled manner.
What do you think? How you achieve the same?
I don't think we should add Logger.flush()
because we would put the burden on the user on when to flush the logs. And I don't think there's a good way to decide when we should flush?
For crashes, it wouldn't help either because we can't know when we'll crash so we can't just flush before the crash. If the app terminates in a controlled manner, then CocoaLumberjack flushes automatically.
If you really want this functionality, you could use the metadata as an escape hatch. If you call logger[metadataKey: "cocoa-lumberjack-flushnow"] = nil
or something, then your logging backend will be able to see this and can invoke the flush. To make it look less ugly, you can add this on Logger
with an extension:
extension Logger {
public func flushNow() {
var dummy = self
dummy[metadataKey: "cocoa-lumberjack-flushnow"] = nil // catch this in your `LogHandler` and call Lumberjack's flushNow
}
}