作者 yangfu

fix: 日志记录异常修改

... ... @@ -23,6 +23,7 @@ require (
github.com/onsi/gomega v1.11.0
github.com/sergi/go-diff v1.2.0 // indirect
github.com/shopspring/decimal v1.2.0
github.com/sirupsen/logrus v1.8.0
github.com/smartystreets/goconvey v1.7.2 // indirect
github.com/stretchr/testify v1.7.0
github.com/tidwall/gjson v1.13.0
... ...
... ... @@ -36,7 +36,7 @@ func main() {
})
log.Logger.AddHook(bw)
redis.InitRedis()
log.Logger.Debug("server start ....")
log.Logger.Info("server start ....")
log.Logger.Debug(fmt.Sprintf("ENABLE_KAFKA_LOG:%v", constant.ENABLE_KAFKA_LOG))
go mqtt.Start()
... ...
... ... @@ -37,7 +37,7 @@ func AutoFlushDeviceDailyRunningRecord(ctx context.Context) error {
t := time.Now().Add(-time.Minute * span)
records, err := redis.GetDeviceDailyAllRecord(t)
if err != nil {
log.Logger.Error(err.Error())
log.Logger.Debug(err.Error())
return err
}
... ...
package log
import (
"github.com/linmadan/egglib-go/log"
"github.com/linmadan/egglib-go/log/logrus"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
)
var Logger log.Logger
//var Logger log.Logger
//
//func init() {
// Logger = logrus.NewLogrusLogger()
// Logger.SetServiceName(constant.SERVICE_NAME)
// Logger.SetLevel(constant.LOG_LEVEL)
//}
var Logger *LogrusLogger
func init() {
Logger = logrus.NewLogrusLogger()
Logger = NewLogrusLogger()
Logger.SetServiceName(constant.SERVICE_NAME)
Logger.SetLevel(constant.LOG_LEVEL)
}
... ...
package log
import (
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/writer"
"io"
"os"
)
func init() {
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetOutput(os.Stdout)
logrus.SetLevel(logrus.WarnLevel)
}
type LogrusLogger struct {
serviceName string
logrus *logrus.Logger
}
func (logger *LogrusLogger) SetServiceName(serviceName string) {
logger.serviceName = serviceName
}
func (logger *LogrusLogger) 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 *LogrusLogger) AddHook(w io.Writer) {
level := logger.logrus.Level
var levels []logrus.Level
// 默认已经添加了一个当前log level的hook,所以此处 level+1
for i := 0; i < (int(level)); i++ {
levels = append(levels, logrus.Level(i))
}
logger.logrus.AddHook(&writer.Hook{
Writer: w,
LogLevels: levels,
})
}
func (logger *LogrusLogger) 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 *LogrusLogger) 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 *LogrusLogger) 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 *LogrusLogger) 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 *LogrusLogger) 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 *LogrusLogger) 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 *LogrusLogger) 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 NewLogrusLogger() *LogrusLogger {
logger := logrus.New()
logger.Formatter = &logrus.JSONFormatter{}
logger.Out = os.Stdout
return &LogrusLogger{
logrus: logger,
}
}
... ...
... ... @@ -2,7 +2,10 @@ package routers
import (
"github.com/beego/beego/v2/server/web"
"github.com/beego/beego/v2/server/web/context"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/beego/controllers"
"net/http"
)
func init() {
... ... @@ -11,4 +14,20 @@ func init() {
web.Router("/TestController/InvokPullPrdMoNewest", &controllers.TestController{}, "Get:InvokPullPrdMoNewest")
web.Router("/test/create-device-collection", &controllers.TestController{}, "Post:CreateDeviceCollection")
web.Get("/test/cache-key/:key", func(ctx *context.Context) {
key := ctx.Input.Query(":key")
if len(key) != 0 {
keys, _ := redis.GetRedis().Keys(key).Result()
ctx.Output.SetStatus(http.StatusOK)
ctx.Output.JSON(keys, false, false)
}
})
web.Get("/test/cache/:key", func(ctx *context.Context) {
key := ctx.Input.Query(":key")
if len(key) != 0 {
value, _ := redis.GetRedis().Get(key).Result()
ctx.Output.SetStatus(http.StatusOK)
ctx.Output.Body([]byte(value))
}
})
}
... ...