feat: 重构日志处理,使用slog包替换原有日志函数,增强日志记录能力

This commit is contained in:
ryan
2026-03-13 15:25:33 +08:00
parent 79a7e024d3
commit 6d58d00c9d
12 changed files with 58 additions and 60 deletions
+5 -2
View File
@@ -3,6 +3,7 @@ package common
import (
"flag"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
@@ -58,12 +59,14 @@ func init() {
var err error
*LogDir, err = filepath.Abs(*LogDir)
if err != nil {
FatalLog(err)
slog.Error("resolve log directory failed", "error", err)
os.Exit(1)
}
if _, err := os.Stat(*LogDir); os.IsNotExist(err) {
err = os.Mkdir(*LogDir, 0777)
if err != nil {
FatalLog(err)
slog.Error("create log directory failed", "error", err)
os.Exit(1)
}
}
}
-19
View File
@@ -192,25 +192,6 @@ func SetupGinLog() {
ensureLogger()
}
func SysLog(s string) {
if !shouldLog(logLevelInfo) {
return
}
ensureLogger().Info(s)
}
func SysError(s string) {
if !shouldLog(logLevelError) {
return
}
ensureLogger().Error(s)
}
func FatalLog(v ...any) {
ensureLogger().Error("fatal error", "details", v)
os.Exit(1)
}
func levelLabel(level slog.Level) string {
switch {
case level <= slog.LevelDebug:
+2 -1
View File
@@ -3,6 +3,7 @@ package common
import (
"context"
"github.com/go-redis/redis/v8"
"log/slog"
"os"
"time"
)
@@ -14,7 +15,7 @@ var RedisEnabled = true
func InitRedisClient() (err error) {
if os.Getenv("REDIS_CONN_STRING") == "" {
RedisEnabled = false
SysLog("REDIS_CONN_STRING not set, Redis is not enabled")
slog.Info("redis disabled because REDIS_CONN_STRING is not set")
return nil
}
opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING"))
+3 -2
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"github.com/google/uuid"
"html/template"
"log/slog"
"net"
"os/exec"
"runtime"
@@ -23,14 +24,14 @@ func OpenBrowser(url string) {
err = exec.Command("open", url).Start()
}
if err != nil {
SysError(err.Error())
slog.Error("open browser failed", "error", err)
}
}
func GetIp() (ip string) {
ips, err := net.InterfaceAddrs()
if err != nil {
SysError(err.Error())
slog.Error("get interface addresses failed", "error", err)
return ip
}