-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
logging.go
59 lines (53 loc) · 1.02 KB
/
logging.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
package main
import (
"bytes"
"io"
"net/http"
"unicode"
)
type indentWriter struct {
w io.Writer
newLine bool
indent []byte
}
func newIndentWriter(w io.Writer, indent string) io.Writer {
return &indentWriter{w, true, []byte(indent)}
}
func (me *indentWriter) Write(p []byte) (n int, err error) {
for len(p) != 0 {
if me.newLine {
_, err = me.w.Write(me.indent)
// We intentionally do not include the inserted indent in the return count due to the
// io.Writer contract.
if err != nil {
return
}
me.newLine = false
}
var nn int
nn, err = me.w.Write(p[:1])
n += nn
if err != nil {
return
}
if p[0] == '\n' {
me.newLine = true
}
p = p[1:]
}
return
}
func requestLogString(r *http.Request) []byte {
var buf bytes.Buffer
r.Write(newIndentWriter(&buf, " "))
return bytes.TrimRightFunc(buf.Bytes(), unicode.IsSpace)
}
func requestUrl(r *http.Request) string {
u := *r.URL
u.Host = r.Host
u.Scheme = "http"
if r.TLS != nil {
u.Scheme = "https"
}
return u.String()
}