-
Notifications
You must be signed in to change notification settings - Fork 375
/
log_helper.go
200 lines (184 loc) · 5.66 KB
/
log_helper.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package common
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"time"
"github.com/gin-gonic/gin"
rotatelogs "github.com/lestrrat/go-file-rotatelogs"
"github.com/pkg/errors"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda-infra/pkg/transport"
"github.com/erda-project/erda-infra/pkg/transport/http"
"github.com/erda-project/erda-infra/pkg/transport/interceptor"
"github.com/erda-project/erda/modules/hepa/common/util"
"github.com/erda-project/erda/modules/hepa/config"
)
// 2016-09-27 09:38:21.541541811 +0200 CEST
// 127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700]
// "GET /apache_pb.gif HTTP/1.0" 200 2326
// "http://www.example.com/start.html"
// "Mozilla/4.08 [en] (Win98; I ;Nav)"
var timeFormat = "2006-01-02 15:04:05.000"
var AccessLog *logrus.Logger
var ErrorLog = logrus.StandardLogger()
func InitLogger() {
AccessLog = logrus.New()
level, err := logrus.ParseLevel(config.LogConf.AccessLevel)
if err != nil {
panic(err)
}
AccessLog.SetLevel(level)
AccessLog.SetOutput(os.Stdout)
if len(config.LogConf.AccessFile) > 0 {
LogFileRotate(
AccessLog,
config.LogConf.AccessFile,
time.Hour*time.Duration(config.LogConf.FileMaxAge),
time.Hour*time.Duration(config.LogConf.FileRotateInteval),
config.LogConf.PrettyPrint,
)
}
level, err = logrus.ParseLevel(config.LogConf.ErrorLevel)
if err != nil {
panic(err)
}
ErrorLog.SetLevel(level)
hook := NewLineHook()
hook.Field = "line"
ErrorLog.AddHook(hook)
ErrorLog.SetOutput(os.Stderr)
if len(config.LogConf.ErrorFile) > 0 {
LogFileRotate(
ErrorLog,
config.LogConf.ErrorFile,
time.Hour*time.Duration(config.LogConf.FileMaxAge),
time.Hour*time.Duration(config.LogConf.FileRotateInteval),
config.LogConf.PrettyPrint,
)
}
}
func AccessLogWrap(log *logrus.Logger) transport.ServiceOption {
return transport.WithHTTPOptions(http.WithInterceptor(func(h interceptor.Handler) interceptor.Handler {
return func(ctx context.Context, req interface{}) (interface{}, error) {
httpReq := http.ContextRequest(ctx)
path := httpReq.URL.RequestURI()
start := time.Now()
reqBody, err := ioutil.ReadAll(httpReq.Body)
if err != nil {
return nil, err
}
httpReq.Body = io.NopCloser(bytes.NewReader(reqBody))
defer func() {
util.DoRecover()
stop := time.Since(start)
latency := int(math.Ceil(float64(stop.Nanoseconds()) / 1000000.0))
clientUserAgent := httpReq.UserAgent()
referer := httpReq.Referer()
hostname, err := os.Hostname()
if err != nil {
hostname = "unknow"
}
entry := logrus.NewEntry(log).WithFields(logrus.Fields{
"hostname": hostname,
"latency": latency, // time to process
"method": httpReq.Method,
"path": path,
"referer": referer,
"userAgent": clientUserAgent,
})
msg := fmt.Sprintf(`[%s %s] [%s]`, httpReq.Method, path, reqBody)
entry.Info(msg)
}()
return h(ctx, req)
}
}))
}
// Logger is the logrus logger handler
func AccessWrap(log *logrus.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
// other handler can change c.Path so:
path := c.Request.URL.RequestURI()
start := time.Now()
c.Next()
stop := time.Since(start)
latency := int(math.Ceil(float64(stop.Nanoseconds()) / 1000000.0))
statusCode := c.Writer.Status()
clientIP := c.ClientIP()
clientUserAgent := c.Request.UserAgent()
referer := c.Request.Referer()
reqBody := c.GetString("reqBody")
respBody := c.GetString("respBody")
hostname, err := os.Hostname()
if err != nil {
hostname = "unknow"
}
dataLength := c.Writer.Size()
if dataLength < 0 {
dataLength = 0
}
entry := logrus.NewEntry(log).WithFields(logrus.Fields{
"hostname": hostname,
"statusCode": statusCode,
"latency": latency, // time to process
"clientIP": clientIP,
"method": c.Request.Method,
"path": path,
"referer": referer,
"dataLength": dataLength,
"userAgent": clientUserAgent,
})
if len(c.Errors) > 0 {
entry.Error(c.Errors.ByType(gin.ErrorTypePrivate).String())
} else {
msg := fmt.Sprintf(`[%s %s] [%s] [%d] [%s]`, c.Request.Method, path, reqBody, statusCode, respBody)
if statusCode > 499 {
entry.Error(msg)
} else if statusCode > 399 {
entry.Warn(msg)
} else {
entry.Info(msg)
}
}
}
}
func LogFileRotate(logger *logrus.Logger, logPath string, maxAge time.Duration, rotationTime time.Duration, prettyPrint bool) {
writer, err := rotatelogs.New(
logPath+".%Y%m%d%H",
rotatelogs.WithMaxAge(maxAge), // 文件最大保存时间
rotatelogs.WithRotationTime(rotationTime), // 日志切割时间间隔
)
if err != nil {
logger.Errorf("config local file system logger error. %+v", errors.WithStack(err))
}
lfHook := lfshook.NewHook(lfshook.WriterMap{
logrus.DebugLevel: writer, // 为不同级别设置不同的输出目的
logrus.InfoLevel: writer,
logrus.WarnLevel: writer,
logrus.ErrorLevel: writer,
logrus.FatalLevel: writer,
logrus.PanicLevel: writer,
}, &logrus.JSONFormatter{
TimestampFormat: timeFormat,
PrettyPrint: prettyPrint,
})
logger.AddHook(lfHook)
}