feat: integrate lumberjack for log file management, enhancing logging capabilities with rotation and compression
This commit is contained in:
@@ -70,6 +70,7 @@ require (
|
|||||||
google.golang.org/api v0.259.0
|
google.golang.org/api v0.259.0
|
||||||
google.golang.org/grpc v1.78.0
|
google.golang.org/grpc v1.78.0
|
||||||
google.golang.org/protobuf v1.36.11
|
google.golang.org/protobuf v1.36.11
|
||||||
|
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
gorm.io/driver/postgres v1.5.11
|
gorm.io/driver/postgres v1.5.11
|
||||||
gorm.io/driver/sqlite v1.6.0
|
gorm.io/driver/sqlite v1.6.0
|
||||||
@@ -308,7 +309,6 @@ require (
|
|||||||
google.golang.org/genproto/googleapis/api v0.0.0-20251202230838-ff82c1b0f217 // indirect
|
google.golang.org/genproto/googleapis/api v0.0.0-20251202230838-ff82c1b0f217 // indirect
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20251222181119-0a764e51fe1b // indirect
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20251222181119-0a764e51fe1b // indirect
|
||||||
gopkg.in/inf.v0 v0.9.1 // indirect
|
gopkg.in/inf.v0 v0.9.1 // indirect
|
||||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
|
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
k8s.io/apimachinery v0.32.3 // indirect
|
k8s.io/apimachinery v0.32.3 // indirect
|
||||||
sigs.k8s.io/yaml v1.4.0 // indirect
|
sigs.k8s.io/yaml v1.4.0 // indirect
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
|
|
||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
"gopkg.in/natefinch/lumberjack.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// appLogger 使用私有实例,避免外部依赖改写 logrus 全局状态导致日志丢失
|
// appLogger 使用私有实例,避免外部依赖改写 logrus 全局状态导致日志丢失
|
||||||
@@ -21,7 +22,7 @@ var appLogger = logrus.New()
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
loggerMu sync.Mutex
|
loggerMu sync.Mutex
|
||||||
activeLogFile *os.File
|
activeLogFile io.WriteCloser
|
||||||
)
|
)
|
||||||
|
|
||||||
// LogLevel 日志级别类型
|
// LogLevel 日志级别类型
|
||||||
@@ -262,14 +263,20 @@ func defaultMacAppLogPath() string {
|
|||||||
return filepath.Join(homeDir, "Library", "Logs", appName, appName+".log")
|
return filepath.Join(homeDir, "Library", "Logs", appName, appName+".log")
|
||||||
}
|
}
|
||||||
|
|
||||||
func openLogFile(logPath string) (*os.File, error) {
|
func openLogFile(logPath string) (io.WriteCloser, error) {
|
||||||
dir := filepath.Dir(logPath)
|
dir := filepath.Dir(logPath)
|
||||||
if dir != "." {
|
if dir != "." {
|
||||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
|
return &lumberjack.Logger{
|
||||||
|
Filename: logPath,
|
||||||
|
MaxSize: 50, // megabytes
|
||||||
|
MaxBackups: 3,
|
||||||
|
MaxAge: 28, // days
|
||||||
|
Compress: true,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加调用者字段
|
// 添加调用者字段
|
||||||
|
|||||||
Reference in New Issue
Block a user