-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
50 lines (45 loc) · 1.06 KB
/
log.go
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
package public
import (
"context"
"github.com/gin-gonic/gin"
"github.com/julianlee107/go-common/lib"
)
//错误日志
func ContextWarning(c context.Context, tag string, m map[string]interface{}) {
v := c.Value("trace")
traceContext, ok := v.(*lib.TraceContext)
if !ok {
traceContext = lib.NewTrace()
}
lib.Log.TagWarning(traceContext, tag, m)
}
//普通日志
func ComLogNotice(c *gin.Context, tag string, m map[string]interface{}) {
traceContext := GetGinTraceContext(c)
lib.Log.TagInfo(traceContext, tag, m)
}
// 从gin的Context中获取数据
func GetGinTraceContext(c *gin.Context) *lib.TraceContext {
// 防御
if c == nil {
return lib.NewTrace()
}
traceContext, exists := c.Get("trace")
if exists {
if tc, ok := traceContext.(*lib.TraceContext); ok {
return tc
}
}
return lib.NewTrace()
}
// 从Context中获取数据
func GetTraceContext(c context.Context) *lib.TraceContext {
if c == nil {
return lib.NewTrace()
}
traceContext := c.Value("trace")
if tc, ok := traceContext.(*lib.TraceContext); ok {
return tc
}
return lib.NewTrace()
}