2022-08-16 23:30:23 +08:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2022-08-30 18:49:07 +08:00
|
|
|
"fmt"
|
2022-12-08 21:40:53 +08:00
|
|
|
"io"
|
|
|
|
"os"
|
2022-08-30 18:49:07 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
2022-08-16 23:30:23 +08:00
|
|
|
|
2023-01-29 16:38:34 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/log"
|
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/configs"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-08-16 23:30:23 +08:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-12-08 21:40:53 +08:00
|
|
|
const (
|
|
|
|
TimeFormat = "2006-01-02 15:04:05"
|
2022-12-20 16:48:20 +08:00
|
|
|
FileTImeFormat = "2006-01-02"
|
2022-12-08 21:40:53 +08:00
|
|
|
RollingTimePattern = "0 0 * * *"
|
|
|
|
)
|
|
|
|
|
2022-08-16 23:30:23 +08:00
|
|
|
func Init() {
|
|
|
|
l := logrus.New()
|
|
|
|
setOutput(l, global.CONF.LogConfig)
|
|
|
|
global.LOG = l
|
2022-12-08 21:40:53 +08:00
|
|
|
global.LOG.Info("init logger successfully")
|
2022-08-16 23:30:23 +08:00
|
|
|
}
|
|
|
|
|
2022-12-08 21:27:49 +08:00
|
|
|
func setOutput(logger *logrus.Logger, config configs.LogConfig) {
|
|
|
|
writer, err := log.NewWriterFromConfig(&log.Config{
|
2023-01-29 16:38:34 +08:00
|
|
|
LogPath: global.CONF.System.LogPath,
|
2022-12-08 21:40:53 +08:00
|
|
|
FileName: config.LogName,
|
2022-12-08 23:34:06 +08:00
|
|
|
TimeTagFormat: FileTImeFormat,
|
2023-03-01 10:42:42 +08:00
|
|
|
MaxRemain: config.MaxBackup,
|
2022-12-08 21:40:53 +08:00
|
|
|
RollingTimePattern: RollingTimePattern,
|
2022-12-08 23:34:06 +08:00
|
|
|
LogSuffix: config.LogSuffix,
|
2022-12-08 21:27:49 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2022-08-16 23:30:23 +08:00
|
|
|
}
|
|
|
|
level, err := logrus.ParseLevel(config.Level)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-12-08 21:40:53 +08:00
|
|
|
fileAndStdoutWriter := io.MultiWriter(writer, os.Stdout)
|
|
|
|
|
|
|
|
logger.SetOutput(fileAndStdoutWriter)
|
2022-12-08 21:27:49 +08:00
|
|
|
logger.SetLevel(level)
|
|
|
|
logger.SetFormatter(new(MineFormatter))
|
2022-08-30 18:49:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type MineFormatter struct{}
|
|
|
|
|
|
|
|
func (s *MineFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
|
|
|
detailInfo := ""
|
|
|
|
if entry.Caller != nil {
|
2022-10-17 16:32:31 +08:00
|
|
|
funcion := strings.ReplaceAll(entry.Caller.Function, "github.com/1Panel-dev/1Panel/backend/", "")
|
2022-08-30 18:49:07 +08:00
|
|
|
detailInfo = fmt.Sprintf("(%s: %d)", funcion, entry.Caller.Line)
|
|
|
|
}
|
|
|
|
if len(entry.Data) == 0 {
|
2022-12-27 17:29:37 +08:00
|
|
|
msg := fmt.Sprintf("[%s] [%s] %s %s \n", time.Now().Format(TimeFormat), strings.ToUpper(entry.Level.String()), entry.Message, detailInfo)
|
2022-08-30 18:49:07 +08:00
|
|
|
return []byte(msg), nil
|
|
|
|
}
|
2022-12-27 17:29:37 +08:00
|
|
|
msg := fmt.Sprintf("[%s] [%s] %s %s {%v} \n", time.Now().Format(TimeFormat), strings.ToUpper(entry.Level.String()), entry.Message, detailInfo, entry.Data)
|
2022-08-30 18:49:07 +08:00
|
|
|
return []byte(msg), nil
|
2022-08-16 23:30:23 +08:00
|
|
|
}
|