Skip to content

Commit b6c89ad

Browse files
committed
device-agent: capture panics, log their stacks
1 parent 0d2f306 commit b6c89ad

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

cmd/device-agent/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func main() {
6767

6868
logDir := filepath.Join(cfg.ConfigDir, logger.LogDir)
6969
log := logger.SetupLogger(cfg.LogLevel, logDir, logger.Agent).WithField("component", "main")
70+
defer logger.CapturePanic(log)
7071

7172
programContext, programCancel := context.WithCancel(context.Background())
7273
handleSignals(log, programCancel)

internal/logger/logger.go

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"io"
55
"os"
66
"path/filepath"
7+
"runtime/debug"
78
"time"
89

910
"github.com/sirupsen/logrus"
@@ -74,3 +75,9 @@ func Setup(level string) *logrus.Logger {
7475

7576
return log
7677
}
78+
79+
func CapturePanic(log logrus.FieldLogger) {
80+
if err := recover(); err != nil {
81+
log.WithField("stack", string(debug.Stack())).Errorf("recovered from panic, %T: %x", err, err)
82+
}
83+
}

0 commit comments

Comments
 (0)