/
dump.go
138 lines (115 loc) · 4.16 KB
/
dump.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
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// aahframework.org/aah source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package aah
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"path/filepath"
"strings"
"aahframework.org/ahttp.v0"
"aahframework.org/config.v0"
"aahframework.org/essentials.v0"
"aahframework.org/log.v0"
)
const (
keyAahRequestBodyBuf = "_aahRequestBodyBuf"
keyAahResponseBodyBuf = "_aahResponseBodyBuf"
)
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// app Unexported methods
//______________________________________________________________________________
func (a *app) initDumpLog() error {
// log file configuration
cfg := config.NewEmpty()
file := a.Config().StringDefault("server.dump_log.file", "")
cfg.SetString("log.receiver", "file")
if ess.IsStrEmpty(file) {
cfg.SetString("log.file", filepath.Join(a.logsDir(), a.binaryFilename()+"-dump.log"))
} else {
abspath, err := filepath.Abs(file)
if err != nil {
return err
}
cfg.SetString("log.file", abspath)
}
cfg.SetString("log.pattern", "%message")
adLog, err := log.New(cfg)
if err != nil {
return err
}
a.dumpLog = &dumpLogger{
a: a,
logger: adLog,
logRequestBody: a.Config().BoolDefault("server.dump_log.request_body", false),
logResponseBody: a.Config().BoolDefault("server.dump_log.response_body", false),
}
return nil
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// dumpLogger
//______________________________________________________________________________
type dumpLogger struct {
a *app
logger *log.Logger
logRequestBody bool
logResponseBody bool
}
func (d *dumpLogger) Dump(ctx *Context) {
buf := acquireBuffer()
defer releaseBuffer(buf)
// Request
uri := fmt.Sprintf("%s://%s%s", ctx.Req.Scheme, ctx.Req.Host, ctx.Req.Path)
if qs := ctx.Req.URL().RawQuery; len(qs) > 0 {
uri += "?" + qs
}
buf.WriteString(fmt.Sprintf("\nURI: %s\n", uri))
buf.WriteString(fmt.Sprintf("METHOD: %s\n", ctx.Req.Method))
buf.WriteString(fmt.Sprintf("PROTO: %s\n", ctx.Req.Proto))
buf.WriteString("HEADERS:\n")
buf.WriteString(d.composeHeaders(ctx.Req.Header) + "\n")
if d.logRequestBody {
buf.WriteString("BODY:\n")
d.writeBody(keyAahRequestBodyBuf, ctx.Req.ContentType().Mime, buf, ctx)
}
buf.WriteString("\n\n-----------------------------------------------------------------------\n\n")
// Response
buf.WriteString(fmt.Sprintf("STATUS: %d %s\n", ctx.Res.Status(), http.StatusText(ctx.Res.Status())))
buf.WriteString(fmt.Sprintf("BYTES WRITTEN: %d\n", ctx.Res.BytesWritten()))
buf.WriteString("HEADERS:\n")
buf.WriteString(d.composeHeaders(ctx.Res.Header()) + "\n")
if d.logResponseBody {
buf.WriteString("BODY:\n")
d.writeBody(keyAahResponseBodyBuf, ctx.Reply().ContType, buf, ctx)
}
buf.WriteString("\n\n=======================================================================")
d.logger.Print(buf.String())
}
func (d *dumpLogger) writeBody(key, ct string, w *bytes.Buffer, ctx *Context) {
cbuf := ctx.Get(key)
if cbuf == nil {
w.WriteString(" ***** NO CONTENT *****")
return
}
b := cbuf.(*bytes.Buffer)
switch stripCharset(ct) {
case ahttp.ContentTypeHTML.Mime, ahttp.ContentTypeForm.Mime,
ahttp.ContentTypeMultipartForm.Mime, ahttp.ContentTypePlainText.Mime:
_, _ = b.WriteTo(w)
case ahttp.ContentTypeJSON.Mime, ahttp.ContentTypeJSONText.Mime:
_ = json.Indent(w, b.Bytes(), "", " ")
case ahttp.ContentTypeXML.Mime, ahttp.ContentTypeXMLText.Mime:
// TODO XML formatting
_, _ = b.WriteTo(w)
}
releaseBuffer(b)
}
func (d *dumpLogger) composeHeaders(hdrs http.Header) string {
var str []string
for _, k := range sortHeaderKeys(hdrs) {
str = append(str, fmt.Sprintf(" %s: %s", k, strings.Join(hdrs[k], ", ")))
}
return strings.Join(str, "\n")
}