logrus.go 3.9 KB
package log

import (
	"fmt"
	"github.com/olivere/elastic/v7"
	"github.com/sirupsen/logrus"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/constant"
	"gopkg.in/sohlich/elogrus.v7"
	"io"
	"os"
)

type ServerLog struct {
	serviceName string
	logrus      *logrus.Logger
}

func (logger *ServerLog) AddHook(write io.Writer) {
	panic("implement me")
}

func (logger *ServerLog) SetServiceName(serviceName string) {
	logger.serviceName = serviceName
}

func (logger *ServerLog) SetLevel(level string) {
	switch level {
	case "trace":
		logger.logrus.Level = logrus.TraceLevel
	case "debug":
		logger.logrus.Level = logrus.DebugLevel
	case "info":
		logger.logrus.Level = logrus.InfoLevel
	case "warn":
		logger.logrus.Level = logrus.WarnLevel
	case "error":
		logger.logrus.Level = logrus.ErrorLevel
	case "fatal":
		logger.logrus.Level = logrus.FatalLevel
	case "panic":
		logger.logrus.Level = logrus.PanicLevel
	default:
		logger.logrus.Level = logrus.DebugLevel
	}
}

func (logger *ServerLog) Trace(msg string, appends ...map[string]interface{}) {
	contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
	for _, append := range appends {
		contextLogger = contextLogger.WithFields(append)
	}
	contextLogger.Trace(msg)
}

func (logger *ServerLog) Debug(msg string, appends ...map[string]interface{}) {
	contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
	for _, append := range appends {
		contextLogger = contextLogger.WithFields(append)
	}
	contextLogger.Debug(msg)
}

func (logger *ServerLog) Info(msg string, appends ...map[string]interface{}) {
	contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
	for _, append := range appends {
		contextLogger = contextLogger.WithFields(append)
	}
	contextLogger.Info(msg)
}

func (logger *ServerLog) Warn(msg string, appends ...map[string]interface{}) {
	contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
	for _, append := range appends {
		contextLogger = contextLogger.WithFields(append)
	}
	contextLogger.Warn(msg)
}

func (logger *ServerLog) Error(msg string, appends ...map[string]interface{}) {
	contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
	for _, append := range appends {
		contextLogger = contextLogger.WithFields(append)
	}
	contextLogger.Error(msg)
}

func (logger *ServerLog) Fatal(msg string, appends ...map[string]interface{}) {
	contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
	for _, append := range appends {
		contextLogger = contextLogger.WithFields(append)
	}
	contextLogger.Fatal(msg)
}

func (logger *ServerLog) Panic(msg string, appends ...map[string]interface{}) {
	contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
	for _, append := range appends {
		contextLogger = contextLogger.WithFields(append)
	}
	contextLogger.Panic(msg)
}

func NewServerLog() *ServerLog {
	logger := logrus.New()
	logger.Formatter = &logrus.JSONFormatter{
		TimestampFormat: "2006-01-02 15:04:05",
		PrettyPrint:     true,
	}
	if constant.LOG_TYPE == "file" {
		file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666)
		if err == nil {
			_ = file.Truncate(1024 * 1024 * 2)
			logger.Out = file
		} else {
			logger.Info("Failed to log to file, using default stderr")
		}
	} else if constant.LOG_TYPE == "es" {
		url := fmt.Sprintf("http://%s:%s", "es.chenzhiying.asia", "80")
		client, err := elastic.NewClient(elastic.SetURL(url), elastic.SetSniff(false))
		if err != nil {
			//log.Panic(err)
			panic(err)
		}
		hook, err := elogrus.NewAsyncElasticHook(client, "es.chenzhiying.asia", logrus.DebugLevel, "boss-appointment")
		if err != nil {
			//log.Panic(err)
			panic(err)
		}
		logger.Hooks.Add(hook)
	} else {
		logger.Out = os.Stdout
	}
	return &ServerLog{
		logrus: logger,
	}
}