first commit
This commit is contained in:
46
log/log.go
Normal file
46
log/log.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"slgserver/config"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
fileDir := config.GetString("log.file_dir", "../log/")
|
||||
maxSize := config.GetInt("log.max_size", 128)
|
||||
maxBackups := config.GetInt("log.max_backups", 30)
|
||||
maxAge := config.GetInt("log.max_age", 7)
|
||||
compress := config.GetBool("log.compress", true)
|
||||
|
||||
sa := strings.Split(filepath.Base(os.Args[0]), ".")
|
||||
fileName := sa[0] + ".log"
|
||||
hook := &lumberjack.Logger{
|
||||
Filename: path.Join(fileDir, fileName), // 日志文件路径
|
||||
MaxSize: maxSize, // 每个日志文件保存的最大尺寸 单位:M
|
||||
MaxBackups: maxBackups, // 日志文件最多保存多少个备份
|
||||
MaxAge: maxAge, // 文件最多保存多少天
|
||||
Compress: compress, // 是否压缩
|
||||
}
|
||||
|
||||
// 创建多写入器:同时写入标准输出和文件
|
||||
multiWriter := io.MultiWriter(os.Stdout, hook)
|
||||
|
||||
// 创建 JSON handler,包含调用者信息
|
||||
opts := &slog.HandlerOptions{
|
||||
Level: slog.LevelInfo,
|
||||
AddSource: true, // 添加调用者信息
|
||||
}
|
||||
|
||||
handler := slog.NewJSONHandler(multiWriter, opts)
|
||||
logger := slog.New(handler)
|
||||
|
||||
// 使用 slog 标准方式设置默认 logger
|
||||
slog.SetDefault(logger)
|
||||
}
|
||||
Reference in New Issue
Block a user