Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/internal/bootstrap/log.go
1560 views
1
package bootstrap
2
3
import (
4
"io"
5
"log"
6
"os"
7
8
"github.com/alist-org/alist/v3/cmd/flags"
9
"github.com/alist-org/alist/v3/internal/conf"
10
"github.com/alist-org/alist/v3/pkg/utils"
11
"github.com/natefinch/lumberjack"
12
"github.com/sirupsen/logrus"
13
)
14
15
func init() {
16
formatter := logrus.TextFormatter{
17
TimestampFormat: "2006-01-02 15:04:05",
18
FullTimestamp: true,
19
}
20
if os.Getenv("NO_COLOR") != "" || os.Getenv("ALIST_NO_COLOR") == "1" {
21
formatter.DisableColors = true
22
} else {
23
formatter.ForceColors = true
24
formatter.EnvironmentOverrideColors = true
25
}
26
logrus.SetFormatter(&formatter)
27
utils.Log.SetFormatter(&formatter)
28
// logrus.SetLevel(logrus.DebugLevel)
29
}
30
31
func setLog(l *logrus.Logger) {
32
if flags.Debug || flags.Dev {
33
l.SetLevel(logrus.DebugLevel)
34
l.SetReportCaller(true)
35
} else {
36
l.SetLevel(logrus.InfoLevel)
37
l.SetReportCaller(false)
38
}
39
}
40
41
func Log() {
42
setLog(logrus.StandardLogger())
43
setLog(utils.Log)
44
logConfig := conf.Conf.Log
45
if logConfig.Enable {
46
var w io.Writer = &lumberjack.Logger{
47
Filename: logConfig.Name,
48
MaxSize: logConfig.MaxSize, // megabytes
49
MaxBackups: logConfig.MaxBackups,
50
MaxAge: logConfig.MaxAge, //days
51
Compress: logConfig.Compress, // disabled by default
52
}
53
if flags.Debug || flags.Dev || flags.LogStd {
54
w = io.MultiWriter(os.Stdout, w)
55
}
56
logrus.SetOutput(w)
57
}
58
log.SetOutput(logrus.StandardLogger().Out)
59
utils.Log.Infof("init logrus...")
60
}
61
62