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

feat: 重定向输出panic日志到panic.log,并增加用户友好的日志模块提示。 #1067

Merged
merged 6 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ require (
github.com/sunshineplan/imgconv v1.1.4
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a
github.com/tdewolff/minify/v2 v2.20.37
github.com/tidwall/buntdb v1.3.1
github.com/tidwall/buntdb v1.3.2
github.com/tidwall/gjson v1.18.0
github.com/tidwall/sjson v1.2.5
github.com/vmihailenco/msgpack v4.0.4+incompatible
github.com/xuri/excelize/v2 v2.8.1
github.com/yuin/goldmark v1.7.4
Expand All @@ -74,6 +76,7 @@ require (
gopkg.in/elazarl/goproxy.v1 v1.0.0-20180725130230-947c36da3153
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/yaml.v3 v3.0.1
moul.io/zapfilter v1.7.0
)

require (
Expand Down Expand Up @@ -148,12 +151,10 @@ require (
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirect
github.com/tdewolff/parse/v2 v2.7.15 // indirect
github.com/tidwall/btree v1.7.0 // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/grect v0.1.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/rtred v0.1.2 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/tidwall/tinyqueue v0.1.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
Expand All @@ -174,7 +175,6 @@ require (
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.7.2 // indirect
modernc.org/sqlite v1.28.0 // indirect
moul.io/zapfilter v1.7.0 // indirect
)

replace (
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,6 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/sunshineplan/imgconv v1.1.4 h1:lViOZUbDIgW8o74naySXJqZOFgXSW1AdU/cdzZRnVTo=
github.com/sunshineplan/imgconv v1.1.4/go.mod h1:Bc4qh4Z+nslcq+Csck01QZgzWvirKUdltRI7vnEAKd8=
github.com/sunshineplan/pdf v1.0.3 h1:Ng+/f35i0jlB87STk6sXaINqhF0JsIyXLZntWWOcGhg=
github.com/sunshineplan/pdf v1.0.3/go.mod h1:4JqkeywDS6kIsqODkNKZ847P2K8eRpSSzf12FTRmUVg=
github.com/sunshineplan/pdf v1.0.7 h1:62xlc079jh4tGLDjiihyyhwVFkn0IsxLyDpHplbG9Ew=
github.com/sunshineplan/pdf v1.0.7/go.mod h1:QsEmZCWBE3uFK8PCrM0pua1WDWLNU77YusiDEcY56OQ=
github.com/sunshineplan/tiff v0.0.0-20220128141034-29b9d69bd906 h1:+yYRCj+PGQNnnen4+/Q7eKD2J87RJs+O39bjtHhPauk=
Expand All @@ -396,13 +394,11 @@ github.com/tdewolff/test v1.0.11-0.20240106005702-7de5f7df4739 h1:IkjBCtQOOjIn03
github.com/tidwall/assert v0.1.0 h1:aWcKyRBUAdLoVebxo95N7+YZVTFF/ASTr7BN4sLP6XI=
github.com/tidwall/btree v1.7.0 h1:L1fkJH/AuEh5zBnnBbmTwQ5Lt+bRJ5A8EWecslvo9iI=
github.com/tidwall/btree v1.7.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
github.com/tidwall/buntdb v1.3.1 h1:HKoDF01/aBhl9RjYtbaLnvX9/OuenwvQiC3OP1CcL4o=
github.com/tidwall/buntdb v1.3.1/go.mod h1:lZZrZUWzlyDJKlLQ6DKAy53LnG7m5kHyrEHvvcDmBpU=
github.com/tidwall/buntdb v1.3.2 h1:qd+IpdEGs0pZci37G4jF51+fSKlkuUTMXuHhXL1AkKg=
github.com/tidwall/buntdb v1.3.2/go.mod h1:lZZrZUWzlyDJKlLQ6DKAy53LnG7m5kHyrEHvvcDmBpU=
github.com/tidwall/gjson v1.9.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.12.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/grect v0.1.4 h1:dA3oIgNgWdSspFzn1kS4S/RDpZFLrIxAZOdJKjYapOg=
Expand Down
11 changes: 8 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sealdice-core/static"
"sealdice-core/utils/crypto"
log "sealdice-core/utils/kratos"
"sealdice-core/utils/paniclog"
)

/**
Expand Down Expand Up @@ -196,6 +197,13 @@ func main() {
if err != nil {
return
}
// 提前到最开始初始化所有日志
// 1. 初始化全局Kartos日志
log.InitZapWithKartosLog(zapcore.Level(opts.LogLevel))
// 2. 初始化全局panic捕获日志
paniclog.InitPanicLog()
PaienNate marked this conversation as resolved.
Show resolved Hide resolved
// 3. 提示日志打印
log.Info("运行日志开始记录,海豹出现故障时可查看 data/main.log 与 data/panic.log 获取更多信息")

if opts.Version {
fmt.Println(dice.VERSION.String())
Expand Down Expand Up @@ -228,9 +236,6 @@ func main() {

_ = os.MkdirAll("./data", 0o755)

// 初始化全局Kartos日志,由于DICE部分已经完全被砍掉了,所以原本的日志等级用来设置控制台展示的日志等级或许更合适
log.InitZapWithKartosLog(zapcore.Level(opts.LogLevel))

// 提早初始化是为了读取ServiceName
diceManager := &dice.DiceManager{}

Expand Down
31 changes: 31 additions & 0 deletions utils/paniclog/paniclog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package paniclog

import (
"fmt"
"io"
"os"
"time"

log "sealdice-core/utils/kratos"
)

func InitPanicLog() {
f, err := os.OpenFile("./data/panic.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
if err != nil {
log.Fatalf("Failed to open log file: %v", err)
}
// Copied from https://github.com/rclone/rclone/tree/master/fs/log
// 这里GPT说,因为使用了APPEND,所以保证了不需要使用SEEK。但是rclone既然这么用了,我决定相信rclone的处理。
_, err = f.Seek(0, io.SeekEnd)
if err != nil {
log.Errorf("Failed to seek log file to end: %v", err)
}
currentTime := time.Now().Format("2006-01-02 15:04:05")
separator := fmt.Sprintf("\n-------- %s --------\n", currentTime)
// 将分割线写入文件
_, err = f.WriteString(separator)
if err != nil {
log.Fatalf("Failed to write separator to log file: %v", err)
}
redirectStderr(f)
}
18 changes: 18 additions & 0 deletions utils/paniclog/redirect_stderr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copied from https://github.com/rclone/rclone/tree/master/fs/log
// Log the panic to the log file - for oses which can't do this

//go:build !windows && !darwin && !dragonfly && !freebsd && !linux && !nacl && !netbsd && !openbsd

package paniclog

import (
"os"

log "sealdice-core/utils/kratos"
)

// redirectStderr to the file passed in
func redirectStderr(f *os.File) {
// 安卓当前还暂时没有什么头绪,看上去rclone也没头绪。
log.Error("Can't redirect stderr to file")
}
22 changes: 22 additions & 0 deletions utils/paniclog/redirect_stderr_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copied from https://github.com/rclone/rclone/tree/master/fs/log
// Log the panic under unix to the log file

//go:build !windows && !solaris && !plan9 && !js

package paniclog

import (
"os"

"golang.org/x/sys/unix"

log "sealdice-core/utils/kratos"
)

// redirectStderr to the file passed in
func redirectStderr(f *os.File) {
err := unix.Dup2(int(f.Fd()), int(os.Stderr.Fd()))
if err != nil {
log.Fatalf("Failed to redirect stderr to file: %v", err)
}
}
41 changes: 41 additions & 0 deletions utils/paniclog/redirect_stderr_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copied from https://github.com/rclone/rclone/tree/master/fs/log
// Log the panic under windows to the log file
//
// Code from minix, via
//
// https://play.golang.org/p/kLtct7lSUg

//go:build windows

package paniclog

import (
"os"
"syscall"

log "sealdice-core/utils/kratos"
)

var (
kernel32 = syscall.MustLoadDLL("kernel32.dll")
procSetStdHandle = kernel32.MustFindProc("SetStdHandle")
)

func setStdHandle(stdhandle int32, handle syscall.Handle) error {
r0, _, e1 := syscall.SyscallN(procSetStdHandle.Addr(), uintptr(stdhandle), uintptr(handle))
if r0 == 0 {
if e1 != 0 {
return error(e1)
}
return syscall.EINVAL
}
return nil
}

// redirectStderr to the file passed in
func redirectStderr(f *os.File) {
err := setStdHandle(syscall.STD_ERROR_HANDLE, syscall.Handle(f.Fd()))
if err != nil {
log.Fatalf("Failed to redirect stderr to file: %v", err)
}
}
Loading