feat: use lumberjack for log rotate

This commit is contained in:
Noah Hsu
2022-08-30 15:22:54 +08:00
parent 59ec17a353
commit 046bbb3a48
8 changed files with 65 additions and 68 deletions

View File

@ -2,12 +2,12 @@ package bootstrap
import (
"github.com/alist-org/alist/v3/internal/aria2"
log "github.com/sirupsen/logrus"
"github.com/alist-org/alist/v3/pkg/utils"
)
func InitAria2() {
go func() {
_, err := aria2.InitClient(2)
log.Errorf("failed to init aria2 client: %+v", err)
utils.Log.Errorf("failed to init aria2 client: %+v", err)
}()
}

View File

@ -2,57 +2,49 @@ package bootstrap
import (
"log"
"time"
"github.com/alist-org/alist/v3/cmd/flags"
"github.com/alist-org/alist/v3/internal/conf"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/natefinch/lumberjack"
"github.com/sirupsen/logrus"
)
func init() {
logrus.SetFormatter(&logrus.TextFormatter{
formatter := logrus.TextFormatter{
ForceColors: true,
EnvironmentOverrideColors: true,
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
})
}
logrus.SetFormatter(&formatter)
utils.Log.SetFormatter(&formatter)
// logrus.SetLevel(logrus.DebugLevel)
}
func setLog(l *logrus.Logger) {
if flags.Debug || flags.Dev {
l.SetLevel(logrus.DebugLevel)
l.SetReportCaller(true)
} else {
l.SetLevel(logrus.InfoLevel)
l.SetReportCaller(false)
}
}
func Log() {
log.SetOutput(logrus.StandardLogger().Out)
if flags.Debug || flags.Dev {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetReportCaller(true)
} else {
logrus.SetLevel(logrus.InfoLevel)
logrus.SetReportCaller(false)
}
setLog(logrus.StandardLogger())
setLog(utils.Log)
logConfig := conf.Conf.Log
if logConfig.Enable {
var (
writer *rotatelogs.RotateLogs
err error
)
if logConfig.Name != "" {
writer, err = rotatelogs.New(
logConfig.Path,
rotatelogs.WithLinkName(logConfig.Name),
rotatelogs.WithRotationCount(logConfig.RotationCount),
rotatelogs.WithRotationTime(time.Duration(logConfig.RotationTime)*time.Hour),
)
} else {
writer, err = rotatelogs.New(
logConfig.Path,
rotatelogs.WithRotationCount(logConfig.RotationCount),
rotatelogs.WithRotationTime(time.Duration(logConfig.RotationTime)*time.Hour),
)
}
if err != nil {
logrus.Fatalf("failed to create rotate logrus: %s", err)
}
logrus.SetOutput(writer)
logrus.SetOutput(&lumberjack.Logger{
Filename: logConfig.Name,
MaxSize: logConfig.MaxSize, // megabytes
MaxBackups: logConfig.MaxBackups,
MaxAge: logConfig.MaxAge, //days
Compress: logConfig.Compress, // disabled by default
})
}
logrus.Infof("init logrus...")
utils.Log.Infof("init logrus...")
}

View File

@ -7,21 +7,21 @@ import (
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/operations"
log "github.com/sirupsen/logrus"
"github.com/alist-org/alist/v3/pkg/utils"
)
func LoadStorages() {
storages, err := db.GetEnabledStorages()
if err != nil {
log.Fatalf("failed get enabled storages: %+v", err)
utils.Log.Fatalf("failed get enabled storages: %+v", err)
}
go func(storages []model.Storage) {
for i := range storages {
err := operations.LoadStorage(context.Background(), storages[i])
if err != nil {
log.Errorf("failed get enabled storages: %+v", err)
utils.Log.Errorf("failed get enabled storages: %+v", err)
} else {
log.Infof("success load storage: [%s], driver: [%s]",
utils.Log.Infof("success load storage: [%s], driver: [%s]",
storages[i].MountPath, storages[i].Driver)
}
}