1Panel/backend/init/log/log.go
2022-08-17 09:37:30 +08:00

35 lines
914 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package log
import (
"path"
"github.com/1Panel-dev/1Panel/configs"
"github.com/1Panel-dev/1Panel/global"
"github.com/natefinch/lumberjack"
"github.com/sirupsen/logrus"
)
func Init() {
l := logrus.New()
setOutput(l, global.CONF.LogConfig)
global.LOG = l
}
func setOutput(log *logrus.Logger, config configs.LogConfig) {
filePath := path.Join(config.Path, config.LogName+config.LogSuffix)
logPrint := &lumberjack.Logger{
Filename: filePath,
MaxSize: config.LogSize, // 日志文件大小,单位是 MB
MaxBackups: config.LogBackup, // 最大过期日志保留个数
MaxAge: config.LogData, // 保留过期文件最大时间,单位 天
Compress: true, // 是否压缩日志默认是不压缩。这里设置为true压缩日志
}
level, err := logrus.ParseLevel(config.Level)
if err != nil {
panic(err)
}
log.SetOutput(logPrint)
log.SetLevel(level)
}