-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
159 lines (117 loc) · 3.23 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package goutil
import (
"context"
"os"
"runtime"
"github.com/gin-gonic/gin"
"github.com/uptrace/opentelemetry-go-extra/otelzap"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type logs struct {
logger *otelzap.Logger
undo func()
telegram TeleService
}
// Logs is a interface of method logging
type Logs interface {
Config(osFile *os.File, createOutput bool)
Info(ctx context.Context, msg string, zapFields ...zapcore.Field)
Warning(ctx context.Context, err error)
Error(ctx context.Context, err error)
Undo()
Sync()
}
func encodeConfig(osFile *os.File, telegram TeleService, createOutput bool) (logs, error) {
encoder := zap.NewProductionEncoderConfig()
encoder.EncodeTime.UnmarshalText([]byte("ISO8601"))
config := zap.NewProductionConfig()
config.EncoderConfig = encoder
config.DisableStacktrace = true
if createOutput {
config.OutputPaths = []string{osFile.Name(), os.Stdout.Name()}
config.ErrorOutputPaths = config.OutputPaths
}
logger, err := config.Build()
if err != nil {
return logs{}, err
}
return logs{
logger: otelzap.New(logger.WithOptions(zap.AddCallerSkip(1)), otelzap.WithCallerDepth(1)),
undo: zap.RedirectStdLog(logger),
telegram: telegram}, nil
}
// NewLog is a function for set up log information in this service
func NewLog(osFile *os.File, telegram TeleService, createOutput bool) (Logs, error) {
logs, err := encodeConfig(osFile, telegram, createOutput)
if err != nil {
return nil, err
}
return &logs, nil
}
func (l *logs) Config(osFile *os.File, createOutput bool) {
logs, err := encodeConfig(osFile, l.telegram, createOutput)
if err != nil {
return
}
l.Sync()
l.Undo()
l.logger = logs.logger
l.undo = logs.undo
}
func (l *logs) Info(ctx context.Context, msg string, zapFields ...zapcore.Field) {
zapcore := zapcoreField(ctx)
if zapFields != nil {
zapcore = append(zapFields, zapcore...)
}
if ctx != nil {
l.logger.InfoContext(ctx, msg, zapcore...)
return
}
l.logger.InfoContext(ctx, msg)
}
func (l *logs) Warning(ctx context.Context, err error) {
msgError := err.Error()
if ctx != nil {
l.logger.WarnContext(ctx, msgError, zapcoreField(ctx)...)
return
}
l.logger.WarnContext(ctx, msgError)
}
func (l *logs) Error(ctx context.Context, err error) {
msgError := err.Error()
_, path, line, _ := runtime.Caller(1)
go l.telegram.SendError(ctx, path, line, msgError)
if ctx != nil {
l.logger.ErrorContext(ctx, msgError, zapcoreField(ctx)...)
return
}
l.logger.ErrorContext(ctx, msgError)
}
func (l *logs) Undo() {
l.undo()
}
func (l *logs) Sync() {
l.logger.Sync()
}
func zapcoreField(ctx context.Context) (zapFields []zapcore.Field) {
res := getContext(ctx)
zapFields = append(zapFields, zap.String("request-id", res.RequestID))
zapFields = append(zapFields, zap.String("method", res.Method))
zapFields = append(zapFields, zap.String("endpoint", res.Endpoint))
return
}
// LogMiddleware ..
func LogMiddleware(logger Logs) func(c *gin.Context) {
// override handler
return func(c *gin.Context) {
// parse gin.Context to context.Context
ctx := ParseContext(c)
// log info request
logger.Info(ctx, "request")
// next handler
c.Next()
// log info response
logger.Info(ctx, "response", zap.Int("status", c.Writer.Status()))
}
}