package utils import ( "github.com/sirupsen/logrus" "gopkg.in/natefinch/lumberjack.v2" "os" "path" ) var ( DefaultLogPath = "./rdlucklog" DefaultLogFile = "eta_task.log" DefaultBinlogPath = "./binlog" DefaultBinlogFile = "binlog.log" ) var FileLog = logrus.New() var Binlog = logrus.New() func init() { logPath := LogPath if logPath == "" { logPath = DefaultLogPath } logFile := LogFile if logFile == "" { logFile = DefaultLogFile } os.MkdirAll(logPath, os.ModePerm) // 打开文件 logFileName := path.Join(logPath, logFile) // 使用滚动压缩方式记录日志 rolling(FileLog, logFileName) //rolling(bLogFileName) // 设置日志输出JSON格式 jsonFormat := new(logrus.JSONFormatter) jsonFormat.DisableHTMLEscape = true FileLog.SetFormatter(jsonFormat) //LogInstance.SetFormatter(&logrus.TextFormatter{}) // 设置日志记录级别 //FileLog.SetLevel(logrus.DebugLevel) //FileLog.Info("abc") initBinlog() } func initBinlog() { //binlog日志 binlogPath := BinLogPath if binlogPath == "" { binlogPath = DefaultBinlogPath } binlogFile := BinLogFile if binlogFile == "" { binlogFile = DefaultBinlogFile } os.MkdirAll(binlogPath, os.ModePerm) // 使用滚动压缩方式记录日志 logFileName := path.Join(binlogPath, binlogFile) rolling(Binlog, logFileName) //rolling(bLogFileName) // 设置日志输出JSON格式 jsonFormat := new(logrus.JSONFormatter) jsonFormat.DisableHTMLEscape = true Binlog.SetFormatter(jsonFormat) } // 日志滚动设置 func rolling(fLog *logrus.Logger, logFile string) { // 设置输出 fLog.SetOutput(&lumberjack.Logger{ Filename: logFile, //日志文件位置 MaxSize: 500, // 单文件最大容量,单位是MB MaxBackups: 3, // 最大保留过期文件个数 MaxAge: 7, // 保留过期文件的最大时间间隔,单位是天 Compress: true, // 是否需要压缩滚动日志, 使用的 gzip 压缩 }) }