package utils import ( "encoding/json" "github.com/beego/beego/v2/core/logs" rotatelogs "github.com/lestrrat-go/file-rotatelogs" "github.com/sirupsen/logrus" "os" "path" "time" ) const ( DefaultLogPath = "./etalogs/filelog" DefaultBinlogPath = "./etalogs/binlog" DefaultDataPath = "./etalogs/datalog" DefaultApiLogPath = "./etalogs/apilog" ) var FileLog = logrus.New() var ApiLog = logrus.New() var FileLogData = logrus.New() var Binlog *logs.BeeLogger func init() { if LogMaxDays == 0 { LogMaxDays = 30 } logPath := LogPath if logPath == "" { logPath = DefaultLogPath } logFile := LogFile if logFile == "" { logFile = "filelog.log" } os.MkdirAll(logPath, os.ModePerm) // 打开文件 logFileName := path.Join(logPath, logFile) rolling(FileLog, logFileName, LogMaxDays) // 设置日志输出JSON格式 jsonFormat := new(logrus.JSONFormatter) jsonFormat.DisableHTMLEscape = true jsonFormat.TimestampFormat = HlbFormatDateTime FileLog.SetFormatter(jsonFormat) FileLog.SetReportCaller(true) //LogInstance.SetFormatter(&logrus.TextFormatter{}) // 设置日志记录级别 //FileLog.SetLevel(logrus.DebugLevel) initBinlog() initApiLog() initFileLogData() } type logConfig struct { FileName string `json:"filename" description:"保存的文件名"` MaxLines int `json:"maxlines" description:"每个文件保存的最大行数,默认值 1000000"` MaxSize int `json:"maxsize" description:"每个文件保存的最大尺寸,默认值是 1 << 28, //256 MB"` Daily bool `json:"daily" description:"是否按照每天 logrotate,默认是 true"` MaxDays int `json:"maxdays" description:"文件最多保存多少天,默认保存 7 天"` Rotate bool `json:"rotate" description:"是否开启 logrotate,默认是 true"` Level int `json:"level" description:"日志保存的时候的级别,默认是 Trace 级别"` Color bool `json:"color" description:"日志是否输出颜色"` //Perm string `json:"perm" description:"日志文件权限"` } func initBinlog() { //binlog日志 //binlog日志 binlogPath := BinLogPath if binlogPath == "" { binlogPath = DefaultBinlogPath } binlogFile := BinLogFile if binlogFile == "" { binlogFile = "binlog.log" } os.MkdirAll(binlogPath, os.ModePerm) logFileName := path.Join(binlogPath, binlogFile) Binlog = logs.NewLogger(1000000) logConf := getDefaultLogConfig() logConf.FileName = logFileName logConf.MaxLines = 10000000 logConf.Rotate = true b, _ := json.Marshal(logConf) Binlog.SetLogger(logs.AdapterFile, string(b)) Binlog.EnableFuncCallDepth(true) } func initApiLog() { logPath := ApiLogPath if logPath == "" { logPath = DefaultApiLogPath } logFile := ApiLogFile if logFile == "" { logFile = "apilog.log" } os.MkdirAll(logPath, os.ModePerm) // 打开文件 logFileName := path.Join(logPath, logFile) rolling(ApiLog, logFileName, LogMaxDays) // 设置日志输出JSON格式 jsonFormat := new(logrus.JSONFormatter) jsonFormat.DisableHTMLEscape = true jsonFormat.TimestampFormat = HlbFormatDateTime ApiLog.SetFormatter(jsonFormat) } func initFileLogData() { logPath := LogDataPath if logPath == "" { logPath = DefaultDataPath } logFile := LogDataFile if logFile == "" { logFile = "datalog.log" } os.MkdirAll(logPath, os.ModePerm) // 打开文件 logFileName := path.Join(logPath, logFile) rolling(FileLogData, logFileName, LogMaxDays) // 设置日志输出JSON格式 jsonFormat := new(logrus.JSONFormatter) jsonFormat.DisableHTMLEscape = true jsonFormat.TimestampFormat = HlbFormatDateTime FileLogData.SetFormatter(jsonFormat) } func getDefaultLogConfig() logConfig { return logConfig{ FileName: "", MaxLines: 0, MaxSize: 1 << 28, Daily: true, MaxDays: LogMaxDays, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-! Rotate: true, Level: logs.LevelTrace, //Perm: "", } } // 日志滚动设置 func rolling(fLog *logrus.Logger, logFileName string, maxAge int) { // 设置输出 if maxAge == 0 { maxAge = LogMaxDays } content, err := rotatelogs.New( logFileName+"-%Y%m%d", //rotatelogs.WithLinkName("./log/cli.log"), // 生成软链,指向最新日志文件 rotatelogs.WithClock(rotatelogs.Local), //MaxAge and RotationCount cannot be both set 两者不能同时设置 rotatelogs.WithRotationSize(1<<28), //256mb rotatelogs.WithMaxAge(time.Duration(maxAge*24)*time.Hour), //clear //rotatelogs.WithRotationCount(5), //number 默认7份 大于7份 或到了清理时间 开始清理 rotatelogs.WithRotationTime(24*time.Hour), //rotate 最小为1分钟轮询。默认60s 低于1分钟就按1分钟来 ) if err != nil { panic("failed to create rotatelogs: " + err.Error()) } fLog.SetOutput(content) return }