mirror of
https://github.com/simon-ding/polaris.git
synced 2026-04-21 19:27:30 +08:00
feat: support log rotation
This commit is contained in:
19
log/log.go
19
log/log.go
@@ -4,6 +4,7 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/natefinch/lumberjack"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
@@ -16,20 +17,22 @@ const dataPath = "./data"
|
||||
func init() {
|
||||
atom = zap.NewAtomicLevel()
|
||||
atom.SetLevel(zap.DebugLevel)
|
||||
filer, _, err := zap.Open(filepath.Join(dataPath, "polaris.log"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
w := zapcore.AddSync(&lumberjack.Logger{
|
||||
Filename: filepath.Join(dataPath, "logs", "polaris.log"),
|
||||
MaxSize: 50, // megabytes
|
||||
MaxBackups: 3,
|
||||
MaxAge: 30, // days
|
||||
})
|
||||
|
||||
consoleEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
|
||||
|
||||
logger := zap.New(zapcore.NewCore(consoleEncoder, zapcore.Lock(filer), atom), zap.AddCallerSkip(1))
|
||||
|
||||
logger := zap.New(zapcore.NewCore(consoleEncoder, w, atom), zap.AddCallerSkip(1))
|
||||
|
||||
sugar = logger.Sugar()
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
func SetLogLevel(l string) {
|
||||
switch strings.TrimSpace(strings.ToLower(l)) {
|
||||
case "debug":
|
||||
|
||||
Reference in New Issue
Block a user