123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package utils
- import (
- "github.com/sirupsen/logrus"
- "gopkg.in/natefinch/lumberjack.v2"
- "os"
- "path"
- )
- const (
- DefaultLogPath = "./rdlucklog"
- DefaultBinlogPath = "./binlog"
- DefaultBinlogFile = "binlog.log"
- )
- var FileLog = logrus.New()
- var Binlog = logrus.New()
- var ApiLog = logrus.New()
- var FileLogData = logrus.New()
- func init() {
- logPath := LogPath
- if logPath == "" {
- logPath = DefaultLogPath
- }
- logFile := LogFile
- if logFile == "" {
- logFile = "eta_api.log"
- }
- os.MkdirAll(logPath, os.ModePerm)
- // 打开文件
- logFileName := path.Join(logPath, logFile)
- // 使用滚动压缩方式记录日志
- rolling(FileLog, logFileName)
- //rolling(bLogFileName)
- // 设置日志输出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)
- //初始化binlog日志
- //FileLog.Info("abc")
- initBinlog()
- initApiLog()
- }
- 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
- jsonFormat.TimestampFormat = HlbFormatDateTime
- Binlog.SetFormatter(jsonFormat)
- }
- func initApiLog() {
- logPath := ApiLogPath
- if logPath == "" {
- if RunMode == "release" {
- logPath = `/data/etalogs/eta_api`
- } else {
- logPath = `./rdlucklog/api`
- }
- }
- logFile := ApiLogFile
- if logFile == "" {
- logFile = "eta_api_api.log"
- }
- os.MkdirAll(logPath, os.ModePerm)
- // 打开文件
- logFileName := path.Join(logPath, logFile)
- // 使用滚动压缩方式记录日志
- rolling(ApiLog, logFileName)
- //rolling(bLogFileName)
- // 设置日志输出JSON格式
- jsonFormat := new(logrus.JSONFormatter)
- jsonFormat.DisableHTMLEscape = true
- jsonFormat.TimestampFormat = HlbFormatDateTime
- ApiLog.SetFormatter(jsonFormat)
- }
- func initFileLogData() {
- logPath := LogDataPath
- if logPath == "" {
- logPath = DefaultLogPath
- }
- logFile := LogDataFile
- if logFile == "" {
- logFile = "eta_api_data.log"
- }
- os.MkdirAll(logPath, os.ModePerm)
- // 打开文件
- logFileName := path.Join(logPath, logFile)
- // 使用滚动压缩方式记录日志
- rolling(FileLogData, logFileName)
- //rolling(bLogFileName)
- // 设置日志输出JSON格式
- jsonFormat := new(logrus.JSONFormatter)
- jsonFormat.DisableHTMLEscape = true
- jsonFormat.TimestampFormat = HlbFormatDateTime
- FileLogData.SetFormatter(jsonFormat)
- }
- // 日志滚动设置
- func rolling(fLog *logrus.Logger, logFile string) {
- // 设置输出
- fLog.SetOutput(&lumberjack.Logger{
- Filename: logFile, //日志文件位置
- MaxSize: 100, // 单文件最大容量,单位是MB
- MaxBackups: 3, // 最大保留过期文件个数
- MaxAge: 7, // 保留过期文件的最大时间间隔,单位是天
- Compress: true, // 是否需要压缩滚动日志, 使用的 gzip 压缩
- LocalTime: true,
- })
- }
|