Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Logger that can be flushed? to better cooperate with CocoaLumberjack #171

Open
mman opened this issue Nov 25, 2020 · 1 comment
Labels
kind/enhancement Improvements to existing feature.

Comments

@mman
Copy link

mman commented Nov 25, 2020

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?

@ktoso ktoso added the kind/enhancement Improvements to existing feature. label Nov 29, 2020
@weissi
Copy link
Member

weissi commented Mar 5, 2021

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
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Improvements to existing feature.
Projects
None yet
Development

No branches or pull requests

3 participants