implement panic log for gateway and launcher

add file logger to gateway

ref issue: #1734

Signed-off-by: Cytown <cytown@gmail.com>
This commit is contained in:
Cytown
2026-03-23 15:40:17 +08:00
parent cff9065084
commit df17684dd4
10 changed files with 163 additions and 7 deletions
+36
View File
@@ -0,0 +1,36 @@
package logger
import (
"fmt"
"os"
"path/filepath"
"runtime/debug"
"time"
)
func InitPanic(filePath string) (func(), error) {
if err := os.MkdirAll(filepath.Dir(filePath), 0o755); err != nil {
return nil, fmt.Errorf("failed to create log directory: %w", err)
}
writer := initPanicFile(filePath)
if writer == nil {
return nil, fmt.Errorf("failed to create log file: %s", filePath)
}
return func() {
defer writer.Close()
if err := recover(); err != nil {
now := time.Now().Format("2006-01-02 15:04:05")
stack := debug.Stack()
logMsg := "\n\n====================\n[" + now + "] PANIC OCCURRED: " + fmt.Sprintf(
"%v",
err,
) + "\n" + string(
stack,
)
writer.Write([]byte(logMsg))
os.Exit(1)
}
}, nil
}