feat: support log rotation

This commit is contained in:
Simon Ding
2024-07-26 13:59:10 +08:00
parent 2cb6a15c0b
commit f5f8434832
4 changed files with 27 additions and 13 deletions

View File

@@ -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":