-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
120 lines (106 loc) · 2.45 KB
/
logger.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package logger
import (
"context"
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"io"
"sync"
)
type Log struct {
*logrus.Entry
sync.Mutex
step int32
}
type TextFormatter = logrus.TextFormatter
type JSONFormatter = logrus.JSONFormatter
type Formatter = logrus.Formatter
type Option func()
const (
Key = "Logger"
ClientIPField = "client_ip"
RequestMethodField = "request_method"
UserAgentField = "user_agent"
URIField = "uri"
StatusField = "Status"
ErrorsField = "Errors"
EndField = "end"
CodeField = "code"
RequestField = "request"
ResponseField = "response"
StartField = "start"
)
// New return a new log object with log start time
func New(opts ...Option) *Log {
for _, opt := range opts {
opt()
}
return &Log{
Entry: logrus.NewEntry(logrus.StandardLogger()),
}
}
func WithFormatter(formatter Formatter) Option {
return func() {
logrus.SetFormatter(formatter)
}
}
func WithOutput(output io.Writer) Option {
return func() {
logrus.SetOutput(output)
}
}
// GetLogger get logger from context
func GetLogger(ctx context.Context) *Log {
loggerCtx := ctx.Value(Key)
if loggerCtx == nil {
goto NewLogger
} else {
logger, ok := loggerCtx.(*Log)
if !ok {
goto NewLogger
} else {
return logger
}
}
NewLogger:
newLogger := New()
ctx = context.WithValue(
ctx,
Key, newLogger)
return newLogger
}
// ToJsonString convert an object into json string to beautify log
// return nil if marshalling error
func ToJsonString(input interface{}) string {
if bytes, err := json.Marshal(input); err == nil {
return string(bytes)
}
return ""
}
func (l *Log) addStep() int32 {
l.Lock()
defer l.Unlock()
l.step += 1
return l.step
}
// AddLog add a new field to log with step = current step + 1
func (l *Log) AddLog(line string, format ...interface{}) *Log {
step := fmt.Sprintf("STEP_%d", l.addStep())
if len(format) > 0 {
logLine := fmt.Sprintf(line, format...)
l.Entry = l.Entry.WithField(step, logLine)
return l
}
l.Entry = l.Entry.WithField(step, line)
return l
}
// WithField add a new key = value to log with key = field, value = value
func (l *Log) WithField(field string, value interface{}) *Log {
l.Entry = l.Entry.WithField(field, value)
return l
}
// WithFields add multiple key/value to log: key1 = value1, key2 = value2
func (l *Log) WithFields(fields map[string]interface{}) *Log {
l.Entry = l.Entry.WithFields(fields)
return l
}