Permalink
Browse files

slog: create a function that will recover panics and log them using s…

…log.Fatal
1 parent 471d760 commit cd411cef755b105e5589c311d74872087c3622dd @gbrayut gbrayut committed Jan 10, 2017
Showing with 9 additions and 0 deletions.
  1. +9 −0 slog/slog.go
View
@@ -11,6 +11,7 @@ import (
"os"
"path/filepath"
"runtime"
+ "runtime/debug"
"strings"
)
@@ -177,3 +178,11 @@ func Wrap(err error) error {
}
return wrappedError{err, line}
}
+
+//Add defer slog.PanicAsFatal() to the top of a goroutine to recover panics and log them using slog.Fatal
+func PanicAsFatal() {
+ if r := recover(); r != nil {
+ s := string(debug.Stack()[:])
+ Fatalf("panic: %s\nStackTrace:\n\n%s", r, s)
+ }
+}

0 comments on commit cd411ce

Please sign in to comment.