-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
266 lines (235 loc) · 5.86 KB
/
main.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package zlog
import (
"bytes"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"os"
"strconv"
"strings"
"time"
caddy "github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/caddyserver/caddy/v2/modules/logging"
"github.com/dustin/go-humanize"
)
const (
DefaultTruncate = 1024
)
func init() {
caddy.RegisterModule(&ZLog{})
httpcaddyfile.RegisterHandlerDirective("zlog", parseCaddyfile)
}
// ZLog 插件打印请求到特定目录
// caddy 插件要求尽量大写
type ZLog struct {
FileWriter logging.FileWriter
LogFile io.WriteCloser
FileName string
Truncate uint64
}
func (z *ZLog) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.zlog",
New: func() caddy.Module {
return &ZLog{}
},
}
}
func (z *ZLog) UnmarshalCaddyfile(d *caddyfile.Dispenser) (err error) {
fw := &z.FileWriter
for d.Next() {
if d.NextArg() {
return d.ArgErr()
}
for d.NextBlock(0) {
switch d.Val() {
case "file_name":
if !d.AllArgs(&fw.Filename) {
return d.ArgErr()
}
z.FileName = fw.Filename
case "roll_disabled":
var f bool
fw.Roll = &f
if d.NextArg() {
return d.ArgErr()
}
case "truncate":
var sizeStr string
if !d.AllArgs(&sizeStr) {
return d.ArgErr()
}
z.Truncate, _ = humanize.ParseBytes(sizeStr)
case "roll_size":
var sizeStr string
if !d.AllArgs(&sizeStr) {
return d.ArgErr()
}
size, err := humanize.ParseBytes(sizeStr)
if err != nil {
return d.Errf("parsing size: %v", err)
}
fw.RollSizeMB = int(math.Ceil(float64(size) / humanize.MiByte))
case "roll_uncompressed":
var f bool
fw.RollCompress = &f
if d.NextArg() {
return d.ArgErr()
}
case "roll_local_time":
fw.RollLocalTime = true
if d.NextArg() {
return d.ArgErr()
}
case "roll_keep":
var keepStr string
if !d.AllArgs(&keepStr) {
return d.ArgErr()
}
keep, err := strconv.Atoi(keepStr)
if err != nil {
return d.Errf("parsing roll_keep number: %v", err)
}
fw.RollKeep = keep
case "roll_keep_for":
var keepForStr string
if !d.AllArgs(&keepForStr) {
return d.ArgErr()
}
keepFor, err := caddy.ParseDuration(keepForStr)
if err != nil {
return d.Errf("parsing roll_keep_for duration: %v", err)
}
if keepFor < 0 {
return d.Errf("negative roll_keep_for duration: %v", keepFor)
}
fw.RollKeepDays = int(math.Ceil(keepFor.Hours() / 24))
}
}
}
if z.Truncate == 0 {
z.Truncate = DefaultTruncate
}
z.printCfg()
return nil
}
type proxyWriter struct {
http.ResponseWriter
respBuf bytes.Buffer
respSize int
code int
req *http.Request
body io.ReadCloser
reqBuf bytes.Buffer
reqSize int
truncate int
}
func (pw *proxyWriter) Read(p []byte) (n int, err error) {
n, err = pw.body.Read(p)
pw.reqSize += n
pw.reqBuf.Write(p[:pw.min(pw.truncate-pw.reqBuf.Len(), n)])
return
}
func (z *ZLog) printCfg() {
data, _ := json.Marshal(z.FileWriter)
fmt.Printf("zlog cfg = %s\n", data)
}
func (pw *proxyWriter) Close() error {
return pw.body.Close()
}
func (p *proxyWriter) WriteHeader(statusCode int) {
p.ResponseWriter.WriteHeader(statusCode)
p.code = statusCode
}
func (p *proxyWriter) min(x, y int) int {
if x < y {
return x
}
return y
}
func (p *proxyWriter) Write(data []byte) (n int, err error) {
n, err = p.ResponseWriter.Write(data)
p.respSize += n
p.respBuf.Write(data[:p.min(len(data), p.truncate-p.respBuf.Len())])
return
}
func (p *proxyWriter) tryToJson(buf bytes.Buffer) (out string) {
bytes := buf.Bytes()
// 发现非 ascii 字符
for i := range bytes {
if bytes[i] > 127 {
return
}
}
out = string(bytes)
var (
jsonObj interface{}
err error
)
if err = json.Unmarshal([]byte(out), &jsonObj); err != nil {
return strings.ReplaceAll(out, "\n", "\\n")
}
data, _ := json.Marshal(jsonObj)
return string(data)
}
func (p *proxyWriter) writeLog(d time.Duration, w io.Writer) {
now := time.Now().Format("2006-01-02 15:04:05")
fmt.Fprintf(w, "%s %s %d %s %s %s", now, d.String(), p.code, p.req.Method, p.req.URL.Path, p.req.Header.Get("Content-Type"))
fmt.Fprintf(w, " [request body %s] %s", humanize.Bytes(uint64(p.reqSize)), p.tryToJson(p.reqBuf))
fmt.Fprintf(w, " %s [response body %s] %s", p.ResponseWriter.Header().Get("Content-Type"), humanize.Bytes(uint64(p.respSize)), p.tryToJson(p.respBuf))
w.Write([]byte(" \n"))
}
// ServeHTTP 打印日志
// 格式 = 时间 + Code + 请求方法 + PATH + HOSTNAME + 路径 + 请求体 + 响应体
func (z *ZLog) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) (err error) {
start := time.Now()
writer := proxyWriter{
ResponseWriter: w,
req: r,
body: r.Body,
truncate: int(z.Truncate),
}
r.Body = &writer
err = next.ServeHTTP(&writer, r)
end := time.Now()
if z.LogFile != nil {
var buf bytes.Buffer
writer.writeLog(end.Sub(start), &buf)
s := buf.String()
z.LogFile.Write([]byte(s))
os.Stdout.Write([]byte(s))
}
return
}
// parseCaddyfile unmarshals tokens from h into a new Middleware.
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var zlog ZLog
err := zlog.UnmarshalCaddyfile(h.Dispenser)
return &zlog, err
}
// Provision implements caddy.Provisioner.
func (z *ZLog) Provision(ctx caddy.Context) error {
z.LogFile, _ = z.FileWriter.OpenWriter()
return nil
}
// Validate implements caddy.Validator.
func (z *ZLog) Validate() error {
return nil
}
func (z *ZLog) Cleanup() error {
if z.LogFile != nil {
z.LogFile.Close()
}
return nil
}
var (
_ caddy.Provisioner = (*ZLog)(nil)
_ caddy.Validator = (*ZLog)(nil)
_ caddyhttp.MiddlewareHandler = (*ZLog)(nil)
_ caddyfile.Unmarshaler = (*ZLog)(nil)
)