Last active
July 1, 2018 02:38
-
-
Save blaskovicz/8a61d655cb5ebaab53f6645204aa51b8 to your computer and use it in GitHub Desktop.
make sure to flush async logrus_sentry hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
logsentry "github.com/evalphobia/logrus_sentry" | |
"github.com/sirupsen/logrus" | |
) | |
type flushSentryHook struct { | |
hook *logsentry.SentryHook | |
} | |
func (f *flushSentryHook) Levels() []logrus.Level { | |
return []logrus.Level{logrus.PanicLevel, logrus.FatalLevel} | |
} | |
func (f *flushSentryHook) Fire(*logrus.Entry) error { | |
// sinc we're async flushing, lets make sure we do a final flush | |
// before we exit the program | |
timeout := time.After(10 * time.Second) | |
flushed := make(chan interface{}, 1) | |
go func() { | |
defer HandlePanic() | |
f.hook.Flush() | |
flushed <- struct{}{} | |
}() | |
select { | |
case <-flushed: | |
return nil | |
case <-timeout: | |
return fmt.Errorf("Timed out after 10s waiting to flush sentry hook") | |
} | |
return nil | |
} | |
func main() { | |
hook, err := logsentry.NewAsyncSentryHook("https://your/sentry/dsn", []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel}) | |
if err != nil { | |
logrus.Fatal("Failed to create logrus sentry hook") | |
return | |
} | |
logrus.AddHook(hook) | |
logrus.AddHook(&flushSentryHook{hook}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Editted with https://www.carlyzach.com/functions/playground/8a61d655cb5ebaab53f6645204aa51b8