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

logrus.Panic and async hook don't work well together #46

Open
glasser opened this issue May 25, 2017 · 3 comments
Open

logrus.Panic and async hook don't work well together #46

glasser opened this issue May 25, 2017 · 3 comments

Comments

@glasser
Copy link
Contributor

glasser commented May 25, 2017

When doing a logrus.Fatal or logrus.Panic, it would be good if an async hook got flushed. With Fatal, this is doable because you can use logrus.RegisterExitHandler to call Flush. But you can't do that for Panic. Is it possible to get a Panic out with an async hook?

I'm thinking maybe Fire should ignore hook.asynchronous for the Panic level (and honestly maybe for the Fatal level too, so you don't have to hook up a Flush).

@glasser
Copy link
Contributor Author

glasser commented May 25, 2017

(I guess you can add a second hook that flushes on Panic and Fatal...)

@glasser
Copy link
Contributor Author

glasser commented May 25, 2017

This is my workaround:

type flushSentryHook struct {
	sentryHook *logrus_sentry.SentryHook
}

func (flushSentryHook) Levels() []logrus.Level {
	return []logrus.Level{logrus.PanicLevel, logrus.FatalLevel}
}
func (fsh flushSentryHook) Fire(*logrus.Entry) error {
	fsh.sentryHook.Flush()
	return nil
}

Then, after adding the sentry hook, also run logrus.AddHook(flushSentryHook{sentryHook: hook}).

@blaskovicz
Copy link

blaskovicz commented Apr 18, 2018

@glasser thanks for the snippet. I also wrapped your final shutdown flush in a timeout of its own (so that the wg.Done() of the underlying async hook doesn't wait forever).

https://gist.github.com/blaskovicz/8a61d655cb5ebaab53f6645204aa51b8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants