alist/internal/bootstrap/log.go

57 lines
1.3 KiB
Go
Raw Normal View History

2022-06-06 13:48:53 +00:00
package bootstrap
import (
2022-12-14 05:19:08 +00:00
"io"
2022-06-14 09:18:58 +00:00
"log"
2022-12-14 05:19:08 +00:00
"os"
2022-06-14 09:18:58 +00:00
2022-08-07 05:09:59 +00:00
"github.com/alist-org/alist/v3/cmd/flags"
2022-08-03 06:26:59 +00:00
"github.com/alist-org/alist/v3/internal/conf"
2022-08-30 07:22:54 +00:00
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/natefinch/lumberjack"
2022-06-06 14:06:33 +00:00
"github.com/sirupsen/logrus"
2022-06-06 13:48:53 +00:00
)
2022-06-14 09:18:58 +00:00
func init() {
2022-08-30 07:22:54 +00:00
formatter := logrus.TextFormatter{
2022-06-06 13:48:53 +00:00
ForceColors: true,
EnvironmentOverrideColors: true,
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
2022-08-30 07:22:54 +00:00
}
logrus.SetFormatter(&formatter)
utils.Log.SetFormatter(&formatter)
2022-08-07 05:23:15 +00:00
// logrus.SetLevel(logrus.DebugLevel)
2022-06-14 09:18:58 +00:00
}
2022-08-30 07:22:54 +00:00
func setLog(l *logrus.Logger) {
2022-08-07 05:09:59 +00:00
if flags.Debug || flags.Dev {
2022-08-30 07:22:54 +00:00
l.SetLevel(logrus.DebugLevel)
l.SetReportCaller(true)
2022-06-26 12:25:02 +00:00
} else {
2022-08-30 07:22:54 +00:00
l.SetLevel(logrus.InfoLevel)
l.SetReportCaller(false)
2022-06-14 09:18:58 +00:00
}
2022-08-30 07:22:54 +00:00
}
func Log() {
setLog(logrus.StandardLogger())
setLog(utils.Log)
2022-06-06 13:48:53 +00:00
logConfig := conf.Conf.Log
2022-06-09 07:12:34 +00:00
if logConfig.Enable {
var w io.Writer = &lumberjack.Logger{
2022-08-30 07:22:54 +00:00
Filename: logConfig.Name,
MaxSize: logConfig.MaxSize, // megabytes
MaxBackups: logConfig.MaxBackups,
MaxAge: logConfig.MaxAge, //days
Compress: logConfig.Compress, // disabled by default
}
if flags.Debug || flags.Dev {
w = io.MultiWriter(os.Stdout, w)
}
logrus.SetOutput(w)
2022-06-06 13:48:53 +00:00
}
log.SetOutput(logrus.StandardLogger().Out)
2022-08-30 07:22:54 +00:00
utils.Log.Infof("init logrus...")
2022-06-06 13:48:53 +00:00
}