From 07d7cf4a40e37df23e0a918c74879cac9620c6c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=BE=E6=81=A9=E7=BA=B3=E7=89=B9?= <1101839859@qq.com> Date: Tue, 15 Oct 2024 20:25:17 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=E9=87=8D=E5=AE=9A=E5=90=91?= =?UTF-8?q?=E8=BE=93=E5=87=BApanic=E6=97=A5=E5=BF=97=E5=88=B0panic.log?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7=E5=8F=8B?= =?UTF-8?q?=E5=A5=BD=E7=9A=84=E6=97=A5=E5=BF=97=E6=A8=A1=E5=9D=97=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 9 +++-- utils/kratos/zap.go | 1 + utils/paniclog/paniclog.go | 33 ++++++++++++++++++ utils/paniclog/redirect_stderr.go | 18 ++++++++++ utils/paniclog/redirect_stderr_unix.go | 22 ++++++++++++ utils/paniclog/redirect_stderr_windows.go | 41 +++++++++++++++++++++++ 6 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 utils/paniclog/paniclog.go create mode 100644 utils/paniclog/redirect_stderr.go create mode 100644 utils/paniclog/redirect_stderr_unix.go create mode 100644 utils/paniclog/redirect_stderr_windows.go diff --git a/main.go b/main.go index 4e793662..8bf35784 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,7 @@ import ( "sealdice-core/static" "sealdice-core/utils/crypto" log "sealdice-core/utils/kratos" + "sealdice-core/utils/paniclog" ) /** @@ -196,6 +197,11 @@ func main() { if err != nil { return } + // 提前到最开始初始化所有日志 + // 1. 初始化全局Kartos日志 + log.InitZapWithKartosLog(zapcore.Level(opts.LogLevel)) + // 2. 初始化全局panic捕获日志 + paniclog.InitPanicLog() if opts.Version { fmt.Println(dice.VERSION.String()) @@ -228,9 +234,6 @@ func main() { _ = os.MkdirAll("./data", 0o755) - // 初始化全局Kartos日志,由于DICE部分已经完全被砍掉了,所以原本的日志等级用来设置控制台展示的日志等级或许更合适 - log.InitZapWithKartosLog(zapcore.Level(opts.LogLevel)) - // 提早初始化是为了读取ServiceName diceManager := &dice.DiceManager{} diff --git a/utils/kratos/zap.go b/utils/kratos/zap.go index 64c8c733..37d97bcf 100644 --- a/utils/kratos/zap.go +++ b/utils/kratos/zap.go @@ -107,6 +107,7 @@ func InitZapWithKartosLog(level zapcore.Level) { // 设置全局日志记录器,默认全局记录器为SEAL命名空间 global.SetLogger(NewZapLogger(originZapLogger.Named(LOG_SEAL))) + Info("海豹全局日志核心已启动。当海豹出现非闪退问题时,可考虑提供data/main.log至开发者或提交至ISSUE.") } func GetWebLogger() *Helper { diff --git a/utils/paniclog/paniclog.go b/utils/paniclog/paniclog.go new file mode 100644 index 00000000..43a6a131 --- /dev/null +++ b/utils/paniclog/paniclog.go @@ -0,0 +1,33 @@ +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) + } + // 重定向 + log.Info("重定向Panic日志模块已启动,当海豹突然豹炸时,请查看data/main.log及data/panic.log,并在必要时将其提供给群友/开发者") + redirectStderr(f) +} diff --git a/utils/paniclog/redirect_stderr.go b/utils/paniclog/redirect_stderr.go new file mode 100644 index 00000000..7b64dac4 --- /dev/null +++ b/utils/paniclog/redirect_stderr.go @@ -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") +} diff --git a/utils/paniclog/redirect_stderr_unix.go b/utils/paniclog/redirect_stderr_unix.go new file mode 100644 index 00000000..e078b9cd --- /dev/null +++ b/utils/paniclog/redirect_stderr_unix.go @@ -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) + } +} diff --git a/utils/paniclog/redirect_stderr_windows.go b/utils/paniclog/redirect_stderr_windows.go new file mode 100644 index 00000000..cfa686e9 --- /dev/null +++ b/utils/paniclog/redirect_stderr_windows.go @@ -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) + } +} From ab13068f5cef8c58f7f30bdd3945ce6d31d027d0 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 15 Oct 2024 14:49:01 +0000 Subject: [PATCH 2/5] chore(deps): bump github.com/tidwall/buntdb from 1.3.1 to 1.3.2 Bumps [github.com/tidwall/buntdb](https://github.com/tidwall/buntdb) from 1.3.1 to 1.3.2. - [Commits](https://github.com/tidwall/buntdb/compare/v1.3.1...v1.3.2) --- updated-dependencies: - dependency-name: github.com/tidwall/buntdb dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- go.mod | 8 ++++---- go.sum | 8 ++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 2e10b700..279f04e5 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 ( @@ -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 @@ -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 ( diff --git a/go.sum b/go.sum index 2051c5a1..20820e23 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= From 3d5b9f9e950d269b1d161276679451b7ff07a22b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=BE=E6=81=A9=E7=BA=B3=E7=89=B9?= <1101839859@qq.com> Date: Tue, 15 Oct 2024 22:54:19 +0800 Subject: [PATCH 3/5] 0 --- go.mod | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 35a73eee..9ef8c7d9 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module sealdice-core -go 1.20 +go 1.21 + +toolchain go1.22.0 require ( github.com/Masterminds/semver/v3 v3.2.1 From ba3588e438809976e12c44c4214fa7d35f3ffeb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=BE=E6=81=A9=E7=BA=B3=E7=89=B9?= <1101839859@qq.com> Date: Tue, 15 Oct 2024 23:03:06 +0800 Subject: [PATCH 4/5] =?UTF-8?q?chores:=20=E4=BF=AE=E6=AD=A3=E9=80=9A?= =?UTF-8?q?=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 2 ++ utils/kratos/zap.go | 1 - utils/paniclog/paniclog.go | 2 -- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 8bf35784..0b41c6a6 100644 --- a/main.go +++ b/main.go @@ -202,6 +202,8 @@ func main() { log.InitZapWithKartosLog(zapcore.Level(opts.LogLevel)) // 2. 初始化全局panic捕获日志 paniclog.InitPanicLog() + // 3. 提示日志打印 + log.Info("运行日志开始记录,海豹出现故障时可查看 data/main.log 与 data/panic.log 获取更多信息") if opts.Version { fmt.Println(dice.VERSION.String()) diff --git a/utils/kratos/zap.go b/utils/kratos/zap.go index 37d97bcf..64c8c733 100644 --- a/utils/kratos/zap.go +++ b/utils/kratos/zap.go @@ -107,7 +107,6 @@ func InitZapWithKartosLog(level zapcore.Level) { // 设置全局日志记录器,默认全局记录器为SEAL命名空间 global.SetLogger(NewZapLogger(originZapLogger.Named(LOG_SEAL))) - Info("海豹全局日志核心已启动。当海豹出现非闪退问题时,可考虑提供data/main.log至开发者或提交至ISSUE.") } func GetWebLogger() *Helper { diff --git a/utils/paniclog/paniclog.go b/utils/paniclog/paniclog.go index 43a6a131..e8aadaa7 100644 --- a/utils/paniclog/paniclog.go +++ b/utils/paniclog/paniclog.go @@ -27,7 +27,5 @@ func InitPanicLog() { if err != nil { log.Fatalf("Failed to write separator to log file: %v", err) } - // 重定向 - log.Info("重定向Panic日志模块已启动,当海豹突然豹炸时,请查看data/main.log及data/panic.log,并在必要时将其提供给群友/开发者") redirectStderr(f) } From 18e5fe050a4ce3140be0d565211561ae9f96e1fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=BE=E6=81=A9=E7=BA=B3=E7=89=B9?= <1101839859@qq.com> Date: Tue, 15 Oct 2024 23:03:31 +0800 Subject: [PATCH 5/5] Revert "0" This reverts commit 3d5b9f9e950d269b1d161276679451b7ff07a22b. --- go.mod | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/go.mod b/go.mod index ff1603ce..279f04e5 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,6 @@ module sealdice-core -go 1.21 - -toolchain go1.22.0 +go 1.20 require ( github.com/Masterminds/semver/v3 v3.2.1