log.go 3.4 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package mylogrus

import (
	rotatelogs "github.com/lestrrat-go/file-rotatelogs"
	"github.com/rifflock/lfshook"
	"github.com/sirupsen/logrus"
	"io"
	"os"
	"path/filepath"
	"time"
)

const logDir = "/var/log/hilo/"

var filenamePrefix string
var MyLog = logrus.New()
17
var RedisLog = logrus.New()
hujiebin's avatar
hujiebin committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

func Info(v interface{}) {
	MyLog.Info("")
}

func init() {
	filenamePrefix = logDir + filepath.Base(os.Args[0]) + "."
	// stderr日志重定向
	MyLog.SetOutput(os.Stdout)
	RewriteStderrFile()

	MyLog.SetFormatter(&logrus.TextFormatter{
		ForceQuote:      false,
		DisableQuote:    true,
		TimestampFormat: "2006-01-02 15:04:05.000",
		FullTimestamp:   true,
	})
	hook := lfshook.NewHook(lfshook.WriterMap{
		logrus.DebugLevel: getLevelWrite(logrus.DebugLevel),
		logrus.InfoLevel:  getLevelWrite(logrus.InfoLevel),
		logrus.WarnLevel:  getLevelWrite(logrus.WarnLevel),
		logrus.ErrorLevel: getLevelWrite(logrus.ErrorLevel),
		logrus.FatalLevel: getLevelWrite(logrus.FatalLevel),
		logrus.PanicLevel: getLevelWrite(logrus.PanicLevel),
	}, &logrus.TextFormatter{ForceQuote: false, DisableQuote: true, TimestampFormat: time.RFC3339Nano})
	MyLog.AddHook(hook)
	MyLog.SetLevel(logrus.InfoLevel)
	MyLog.SetReportCaller(true)
}

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
func InitRedisLog() {
	RedisLog.SetOutput(os.Stdout)

	RedisLog.SetFormatter(&logrus.TextFormatter{
		ForceQuote:      false,
		DisableQuote:    true,
		TimestampFormat: "2006-01-02 15:04:05.000",
		FullTimestamp:   true,
	})
	hook := lfshook.NewHook(lfshook.WriterMap{
		logrus.InfoLevel: GetRedisLog(),
	}, &logrus.TextFormatter{ForceQuote: false, DisableQuote: true, TimestampFormat: time.RFC3339Nano})
	RedisLog.AddHook(hook)
	RedisLog.SetLevel(logrus.InfoLevel)
	RedisLog.SetReportCaller(true)
}

hujiebin's avatar
hujiebin committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
func GetInfoLog() io.Writer {
	return getLevelWrite(logrus.InfoLevel)
}

func getLevelWrite(level logrus.Level) io.Writer {
	var name string
	switch level {
	case logrus.DebugLevel:
		name = "debug.log"
	case logrus.InfoLevel:
		name = "info.log"
	case logrus.WarnLevel:
		name = "warn.log"
	case logrus.ErrorLevel:
		name = "error.log"
	case logrus.FatalLevel:
		name = "fatal.log"
	case logrus.PanicLevel:
		name = "panic.log"
	}
	name = filenamePrefix + name
	writer, err := rotatelogs.New(
		name+".%Y%m%d%H",
		rotatelogs.WithLinkName(name),          // 生成软链,指向最新日志文件
		rotatelogs.WithMaxAge(7*24*time.Hour),  // 文件最大保存时间
		rotatelogs.WithRotationTime(time.Hour), // 日志切割时间间隔
	)
	if err != nil {
		MyLog.Fatal("Failed to create log file:", err.Error())
	}
	return writer
}

func GetSqlLog() io.Writer {
	var name string = "sql.log"
	name = filenamePrefix + name
	writer, err := rotatelogs.New(
		name+".%Y%m%d%H",
		rotatelogs.WithLinkName(name),          // 生成软链,指向最新日志文件
		rotatelogs.WithMaxAge(7*24*time.Hour),  // 文件最大保存时间
		rotatelogs.WithRotationTime(time.Hour), // 日志切割时间间隔
	)
	if err != nil {
		MyLog.Fatal("Failed to create log file:", err.Error())
	}
	return writer
}
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

func GetRedisLog() io.Writer {
	var name string = "redis.log"
	name = filenamePrefix + name
	writer, err := rotatelogs.New(
		name+".%Y%m%d%H",
		rotatelogs.WithLinkName(name),          // 生成软链,指向最新日志文件
		rotatelogs.WithMaxAge(7*24*time.Hour),  // 文件最大保存时间
		rotatelogs.WithRotationTime(time.Hour), // 日志切割时间间隔
	)
	if err != nil {
		MyLog.Fatal("Failed to create log file:", err.Error())
	}
	return writer
}