正在显示
6 个修改的文件
包含
154 行增加
和
6 行删除
@@ -23,6 +23,7 @@ require ( | @@ -23,6 +23,7 @@ require ( | ||
23 | github.com/onsi/gomega v1.11.0 | 23 | github.com/onsi/gomega v1.11.0 |
24 | github.com/sergi/go-diff v1.2.0 // indirect | 24 | github.com/sergi/go-diff v1.2.0 // indirect |
25 | github.com/shopspring/decimal v1.2.0 | 25 | github.com/shopspring/decimal v1.2.0 |
26 | + github.com/sirupsen/logrus v1.8.0 | ||
26 | github.com/smartystreets/goconvey v1.7.2 // indirect | 27 | github.com/smartystreets/goconvey v1.7.2 // indirect |
27 | github.com/stretchr/testify v1.7.0 | 28 | github.com/stretchr/testify v1.7.0 |
28 | github.com/tidwall/gjson v1.13.0 | 29 | github.com/tidwall/gjson v1.13.0 |
@@ -36,7 +36,7 @@ func main() { | @@ -36,7 +36,7 @@ func main() { | ||
36 | }) | 36 | }) |
37 | log.Logger.AddHook(bw) | 37 | log.Logger.AddHook(bw) |
38 | redis.InitRedis() | 38 | redis.InitRedis() |
39 | - log.Logger.Debug("server start ....") | 39 | + log.Logger.Info("server start ....") |
40 | log.Logger.Debug(fmt.Sprintf("ENABLE_KAFKA_LOG:%v", constant.ENABLE_KAFKA_LOG)) | 40 | log.Logger.Debug(fmt.Sprintf("ENABLE_KAFKA_LOG:%v", constant.ENABLE_KAFKA_LOG)) |
41 | 41 | ||
42 | go mqtt.Start() | 42 | go mqtt.Start() |
@@ -37,7 +37,7 @@ func AutoFlushDeviceDailyRunningRecord(ctx context.Context) error { | @@ -37,7 +37,7 @@ func AutoFlushDeviceDailyRunningRecord(ctx context.Context) error { | ||
37 | t := time.Now().Add(-time.Minute * span) | 37 | t := time.Now().Add(-time.Minute * span) |
38 | records, err := redis.GetDeviceDailyAllRecord(t) | 38 | records, err := redis.GetDeviceDailyAllRecord(t) |
39 | if err != nil { | 39 | if err != nil { |
40 | - log.Logger.Error(err.Error()) | 40 | + log.Logger.Debug(err.Error()) |
41 | return err | 41 | return err |
42 | } | 42 | } |
43 | 43 |
1 | package log | 1 | package log |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | - "github.com/linmadan/egglib-go/log" | ||
5 | - "github.com/linmadan/egglib-go/log/logrus" | ||
6 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" | 4 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" |
7 | ) | 5 | ) |
8 | 6 | ||
9 | -var Logger log.Logger | 7 | +//var Logger log.Logger |
8 | +// | ||
9 | +//func init() { | ||
10 | +// Logger = logrus.NewLogrusLogger() | ||
11 | +// Logger.SetServiceName(constant.SERVICE_NAME) | ||
12 | +// Logger.SetLevel(constant.LOG_LEVEL) | ||
13 | +//} | ||
14 | + | ||
15 | +var Logger *LogrusLogger | ||
10 | 16 | ||
11 | func init() { | 17 | func init() { |
12 | - Logger = logrus.NewLogrusLogger() | 18 | + Logger = NewLogrusLogger() |
13 | Logger.SetServiceName(constant.SERVICE_NAME) | 19 | Logger.SetServiceName(constant.SERVICE_NAME) |
14 | Logger.SetLevel(constant.LOG_LEVEL) | 20 | Logger.SetLevel(constant.LOG_LEVEL) |
15 | } | 21 | } |
pkg/log/logrus.go
0 → 100644
1 | +package log | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/sirupsen/logrus" | ||
5 | + "github.com/sirupsen/logrus/hooks/writer" | ||
6 | + "io" | ||
7 | + "os" | ||
8 | +) | ||
9 | + | ||
10 | +func init() { | ||
11 | + logrus.SetFormatter(&logrus.JSONFormatter{}) | ||
12 | + logrus.SetOutput(os.Stdout) | ||
13 | + logrus.SetLevel(logrus.WarnLevel) | ||
14 | +} | ||
15 | + | ||
16 | +type LogrusLogger struct { | ||
17 | + serviceName string | ||
18 | + logrus *logrus.Logger | ||
19 | +} | ||
20 | + | ||
21 | +func (logger *LogrusLogger) SetServiceName(serviceName string) { | ||
22 | + logger.serviceName = serviceName | ||
23 | +} | ||
24 | + | ||
25 | +func (logger *LogrusLogger) SetLevel(level string) { | ||
26 | + switch level { | ||
27 | + case "trace": | ||
28 | + logger.logrus.Level = logrus.TraceLevel | ||
29 | + case "debug": | ||
30 | + logger.logrus.Level = logrus.DebugLevel | ||
31 | + case "info": | ||
32 | + logger.logrus.Level = logrus.InfoLevel | ||
33 | + case "warn": | ||
34 | + logger.logrus.Level = logrus.WarnLevel | ||
35 | + case "error": | ||
36 | + logger.logrus.Level = logrus.ErrorLevel | ||
37 | + case "fatal": | ||
38 | + logger.logrus.Level = logrus.FatalLevel | ||
39 | + case "panic": | ||
40 | + logger.logrus.Level = logrus.PanicLevel | ||
41 | + default: | ||
42 | + logger.logrus.Level = logrus.DebugLevel | ||
43 | + } | ||
44 | +} | ||
45 | + | ||
46 | +func (logger *LogrusLogger) AddHook(w io.Writer) { | ||
47 | + level := logger.logrus.Level | ||
48 | + var levels []logrus.Level | ||
49 | + // 默认已经添加了一个当前log level的hook,所以此处 level+1 | ||
50 | + for i := 0; i < (int(level)); i++ { | ||
51 | + levels = append(levels, logrus.Level(i)) | ||
52 | + } | ||
53 | + logger.logrus.AddHook(&writer.Hook{ | ||
54 | + Writer: w, | ||
55 | + LogLevels: levels, | ||
56 | + }) | ||
57 | +} | ||
58 | + | ||
59 | +func (logger *LogrusLogger) Trace(msg string, appends ...map[string]interface{}) { | ||
60 | + contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
61 | + for _, append := range appends { | ||
62 | + contextLogger = contextLogger.WithFields(append) | ||
63 | + } | ||
64 | + contextLogger.Trace(msg) | ||
65 | +} | ||
66 | + | ||
67 | +func (logger *LogrusLogger) Debug(msg string, appends ...map[string]interface{}) { | ||
68 | + contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
69 | + for _, append := range appends { | ||
70 | + contextLogger = contextLogger.WithFields(append) | ||
71 | + } | ||
72 | + contextLogger.Debug(msg) | ||
73 | +} | ||
74 | + | ||
75 | +func (logger *LogrusLogger) Info(msg string, appends ...map[string]interface{}) { | ||
76 | + contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
77 | + for _, append := range appends { | ||
78 | + contextLogger = contextLogger.WithFields(append) | ||
79 | + } | ||
80 | + contextLogger.Info(msg) | ||
81 | +} | ||
82 | + | ||
83 | +func (logger *LogrusLogger) Warn(msg string, appends ...map[string]interface{}) { | ||
84 | + contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
85 | + for _, append := range appends { | ||
86 | + contextLogger = contextLogger.WithFields(append) | ||
87 | + } | ||
88 | + contextLogger.Warn(msg) | ||
89 | +} | ||
90 | + | ||
91 | +func (logger *LogrusLogger) Error(msg string, appends ...map[string]interface{}) { | ||
92 | + contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
93 | + for _, append := range appends { | ||
94 | + contextLogger = contextLogger.WithFields(append) | ||
95 | + } | ||
96 | + contextLogger.Error(msg) | ||
97 | +} | ||
98 | + | ||
99 | +func (logger *LogrusLogger) Fatal(msg string, appends ...map[string]interface{}) { | ||
100 | + contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
101 | + for _, append := range appends { | ||
102 | + contextLogger = contextLogger.WithFields(append) | ||
103 | + } | ||
104 | + contextLogger.Fatal(msg) | ||
105 | +} | ||
106 | + | ||
107 | +func (logger *LogrusLogger) Panic(msg string, appends ...map[string]interface{}) { | ||
108 | + contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
109 | + for _, append := range appends { | ||
110 | + contextLogger = contextLogger.WithFields(append) | ||
111 | + } | ||
112 | + contextLogger.Panic(msg) | ||
113 | +} | ||
114 | + | ||
115 | +func NewLogrusLogger() *LogrusLogger { | ||
116 | + logger := logrus.New() | ||
117 | + logger.Formatter = &logrus.JSONFormatter{} | ||
118 | + logger.Out = os.Stdout | ||
119 | + return &LogrusLogger{ | ||
120 | + logrus: logger, | ||
121 | + } | ||
122 | +} |
@@ -2,7 +2,10 @@ package routers | @@ -2,7 +2,10 @@ package routers | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "github.com/beego/beego/v2/server/web" | 4 | "github.com/beego/beego/v2/server/web" |
5 | + "github.com/beego/beego/v2/server/web/context" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis" | ||
5 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/beego/controllers" | 7 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/beego/controllers" |
8 | + "net/http" | ||
6 | ) | 9 | ) |
7 | 10 | ||
8 | func init() { | 11 | func init() { |
@@ -11,4 +14,20 @@ func init() { | @@ -11,4 +14,20 @@ func init() { | ||
11 | web.Router("/TestController/InvokPullPrdMoNewest", &controllers.TestController{}, "Get:InvokPullPrdMoNewest") | 14 | web.Router("/TestController/InvokPullPrdMoNewest", &controllers.TestController{}, "Get:InvokPullPrdMoNewest") |
12 | 15 | ||
13 | web.Router("/test/create-device-collection", &controllers.TestController{}, "Post:CreateDeviceCollection") | 16 | web.Router("/test/create-device-collection", &controllers.TestController{}, "Post:CreateDeviceCollection") |
17 | + web.Get("/test/cache-key/:key", func(ctx *context.Context) { | ||
18 | + key := ctx.Input.Query(":key") | ||
19 | + if len(key) != 0 { | ||
20 | + keys, _ := redis.GetRedis().Keys(key).Result() | ||
21 | + ctx.Output.SetStatus(http.StatusOK) | ||
22 | + ctx.Output.JSON(keys, false, false) | ||
23 | + } | ||
24 | + }) | ||
25 | + web.Get("/test/cache/:key", func(ctx *context.Context) { | ||
26 | + key := ctx.Input.Query(":key") | ||
27 | + if len(key) != 0 { | ||
28 | + value, _ := redis.GetRedis().Get(key).Result() | ||
29 | + ctx.Output.SetStatus(http.StatusOK) | ||
30 | + ctx.Output.Body([]byte(value)) | ||
31 | + } | ||
32 | + }) | ||
14 | } | 33 | } |
-
请 注册 或 登录 后发表评论