log.go 3.59 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
package mylogrus

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

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

var filenamePrefix string
var MyLog = logrus.New()

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

func init() {
	filenamePrefix = logDir + filepath.Base(os.Args[0]) + "."
	//if runtime.GOOS == "darwin" { // mac本地调试
	//	filenamePrefix = "./log/hilo/" + filepath.Base(os.Args[0]) + "."
	//}
	// stderr日志重定向
	MyLog.SetOutput(os.Stdout)
	RewriteStderrFile()

	if config.AppIsRelease() {
		MyLog.SetFormatter(&logrus.JSONFormatter{
			DisableHTMLEscape: true,
			TimestampFormat:   "2006-01-02 15:04:05.000",
		})
		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.JSONFormatter{DisableHTMLEscape: true, TimestampFormat: time.RFC3339Nano})

		MyLog.AddHook(hook)
		MyLog.SetLevel(logrus.InfoLevel)
		MyLog.SetReportCaller(true)
	} else {
		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)
	}
}

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 {
	//if !config.AppIsRelease() {
	//	return GetInfoLog()
	//}

	//name := filenamePrefix + "sql.log"
	//file, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	//if err == nil {
	//	return file
	//} else {
	//	MyLog.Fatal("Failed to create sql log file:", err.Error())
	//}
	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
}