Skip to content

Commit

Permalink
more reliable crash logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Karmenzind committed Jan 26, 2025
1 parent 6dfd252 commit 00022e9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ freq_alert = false

# 日志配置
[logging]
# 开启日志记录
enable = false
# 开启日志记录(程序异常时会记录关键信息,不建议关闭)
enable = true
# 默认值:Linux/MacOS为/tmp/kd_<username>.log,windows为%TMPDIR%/kd_<username>.log
path = ""
# 日志级别,支持:DEBUG/INFO/WARN/PANIC/FATAL
Expand Down
12 changes: 9 additions & 3 deletions cmd/kd.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ func flagUpdate(ctx *cli.Context, _ bool) (err error) {

if doUpdate {
// emoji.Println(":lightning: Let's update now")
err = daemon.KillDaemonIfRunning()
if err != nil {
if err = daemon.KillDaemonIfRunning(); err != nil {
warnMsg := "可能会影响后续文件替换。如果出现问题,请手动执行`kd --stop`后重试"
d.EchoWarn("停止守护进程出现异常(%s),%s", err, warnMsg)
if p, perr := daemon.FindServerProcess(); perr == nil {
Expand Down Expand Up @@ -272,7 +271,14 @@ func main() {
if err != nil {
d.EchoFatal(err.Error())
}
defer l.Sync()
defer func() {
if r := recover(); r != nil {
zap.S().Errorln("Application crashed", zap.Any("reason", r))
if syncErr := l.Sync(); syncErr != nil {
fmt.Printf("Failed to sync logger: %v\n", syncErr)
}
}
}()
}
zap.S().Debugf("Got configuration: %+v", cfg)
zap.S().Debugf("Got run info: %+v", run.Info)
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var CONFIG_PATH string
type LoggerConfig struct {
Enable bool `default:"true" toml:"enable"`
Path string `toml:"path"`
Level string `default:"info" toml:"level"`
Level string `default:"warn" toml:"level"`
Stderr bool `default:"false" toml:"stderr"`
RedirectToStream bool `default:"false" toml:"redirect_to_stream"`
}
Expand Down
7 changes: 3 additions & 4 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logger

import (
"fmt"
"log"
"os"
"os/user"
"path/filepath"
Expand All @@ -22,8 +23,7 @@ func buildLogger(logCfg *config.LoggerConfig, options ...zap.Option) (*zap.Logge
} else {
var f string
if logCfg.Path == "" {
u, err := user.Current()
if err != nil {
if u, err := user.Current(); err != nil {
f = filepath.Join(os.TempDir(), "kd.log")
} else {
name := strings.ReplaceAll(u.Username, " ", "_")
Expand Down Expand Up @@ -52,10 +52,9 @@ func buildLogger(logCfg *config.LoggerConfig, options ...zap.Option) (*zap.Logge
func InitLogger(logCfg *config.LoggerConfig) (*zap.Logger, error) {
l, err := buildLogger(logCfg)
if err != nil {
panic(err)
log.Panicln(err)
}

zap.ReplaceGlobals(l)

return l, err
}

0 comments on commit 00022e9

Please sign in to comment.