/
context_log.go
91 lines (69 loc) · 1.75 KB
/
context_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
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
package xlog
import (
"bytes"
"context"
"sync"
"github.com/sirupsen/logrus"
)
type ctxLogKey struct{}
var (
_ctxLogKey ctxLogKey
bufPool = &sync.Pool{
New: func() any {
return new(bytes.Buffer)
},
}
)
// func OrganizeLogMiddleware(c entity.Context, in any, ret entity.RetFunc, next entity.Handler) (out any, e error) {
// if _organizeLog {
// _, ok := c.Value(_ctxLogKey).(*logrus.Entry)
// if ok {
// next(c, in, ret)
// return
// }
// clog, buf := newCtxLog(c)
// defer func() {
// organizeLog(c, buf)
// }()
// c = context.WithValue(c, _ctxLogKey, clog)
// }
// next(c, in, ret)
// return
// }
// func organizeLog(c entity.Context, buf *bytes.Buffer) {
// bufStr := buf.String()
// if len(bufStr) == 0 {
// return
// }
// tlog := logrus.NewEntry(logrus.New()).WithContext(c)
// tlog.Logger.SetReportCaller(false)
// tlog.Logger.SetOutput(logrus.StandardLogger().Out)
// tlog.Logger.SetFormatter(organizeLogFormatter)
// tlog.Println("{\n" + bufStr + "}")
// buf.Reset()
// bufPool.Put(buf)
// }
// func newCtxLog(c entity.Context) (*logrus.Entry, *bytes.Buffer) {
// logger := logrus.New()
// stdLogger := logrus.StandardLogger()
// // logger.SetOutput(stdLogger.Out)
// logger.SetLevel(stdLogger.Level)
// logger.SetFormatter(stdLogger.Formatter)
// logger.SetReportCaller(stdLogger.ReportCaller)
// buf, _ := bufPool.Get().(*bytes.Buffer)
// buf.Reset()
// logger.SetOutput(buf)
// for _, hook := range AddedHooks {
// logger.AddHook(hook)
// }
// return logrus.NewEntry(logger).WithContext(c), buf
// }
func log(c context.Context) *logrus.Entry {
if c != nil {
log, ok := c.Value(_ctxLogKey).(*logrus.Entry)
if ok {
return log.WithContext(c)
}
}
return logrus.WithContext(c)
}