共计 1902 个字符,预计需要花费 5 分钟才能阅读完成。
应用
logrus
和go-file-rotatelogs
按天切割日志,源码:iris-framework
package config
import (
config "IrisFramework/config/Log"
rotatelogs "github.com/lestrrat/go-file-rotatelogs"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
"os"
"path"
"time"
)
const LogPath = "./storage/logs"
const FileSuffix = ".log"
var Log = logrus.New()
func InitLog() {
Log.Out = os.Stdout
var loglevel logrus.Level
err := loglevel.UnmarshalText([]byte("info"))
if err != nil {Log.Panicf("设置 log 级别失败:%v", err)
}
Log.SetLevel(loglevel)
NewSimpleLogger(Log, LogPath, 8)
}
/**
文件日志
*/
func NewSimpleLogger(log *logrus.Logger, logPath string, save uint) {
lfHook := lfshook.NewHook(lfshook.WriterMap{logrus.DebugLevel: writer(logPath, "debug", save), // 为不同级别设置不同的输入目标
logrus.InfoLevel: writer(logPath, "info", save),
logrus.WarnLevel: writer(logPath, "warn", save),
logrus.ErrorLevel: writer(logPath, "error", save),
logrus.FatalLevel: writer(logPath, "fatal", save),
logrus.PanicLevel: writer(logPath, "panic", save),
}, &config.MineFormatter{})
log.AddHook(lfHook)
}
/**
文件设置
*/
func writer(logPath string, level string, save uint) *rotatelogs.RotateLogs {logFullPath := path.Join(logPath, level)
var cstSh, _ = time.LoadLocation("Asia/Shanghai") // 上海
fileSuffix := time.Now().In(cstSh).Format("2006-01-02") + FileSuffix
logier, err := rotatelogs.New(
logFullPath+"-"+fileSuffix,
rotatelogs.WithLinkName(logFullPath), // 生成软链,指向最新日志文件
rotatelogs.WithRotationCount(int(save)), // 文件最大保留份数
rotatelogs.WithRotationTime(time.Hour*24), // 日志切割工夫距离
)
if err != nil {panic(err)
}
return logier
}
自定义日志输入格局
package config
import (
"fmt"
"github.com/sirupsen/logrus"
"strings"
"time"
)
type MineFormatter struct{}
const TimeFormat = "2006-01-02 15:04:05"
func (s *MineFormatter) Format(entry *logrus.Entry) ([]byte, error) {msg := fmt.Sprintf("[%s] [%s] %s\n", time.Now().Local().Format(TimeFormat), strings.ToUpper(entry.Level.String()), entry.Message)
return []byte(msg), nil
}
打印输出如下:
[2020-08-05 11:34:08] [INFO] [gorm] [1.604712ms] SELECT * FROM `user` WHERE `user`.`deleted_at` IS NULL AND ((username like ?)) []interface {}{"% 风 %"}
正文完