-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzerologformater.go
132 lines (110 loc) · 3.02 KB
/
zerologformater.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
package zeroformater
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"runtime"
"time"
"github.com/go-chi/chi/middleware"
"github.com/rs/zerolog"
)
var isTTY bool
var (
nRed = []byte{'\033', '[', '3', '1', 'm'}
nGreen = []byte{'\033', '[', '3', '2', 'm'}
nYellow = []byte{'\033', '[', '3', '3', 'm'}
nCyan = []byte{'\033', '[', '3', '6', 'm'}
bRed = []byte{'\033', '[', '3', '1', ';', '1', 'm'}
bGreen = []byte{'\033', '[', '3', '2', ';', '1', 'm'}
bYellow = []byte{'\033', '[', '3', '3', ';', '1', 'm'}
bBlue = []byte{'\033', '[', '3', '4', ';', '1', 'm'}
bMagenta = []byte{'\033', '[', '3', '5', ';', '1', 'm'}
bCyan = []byte{'\033', '[', '3', '6', ';', '1', 'm'}
reset = []byte{'\033', '[', '0', 'm'}
)
var (
_ middleware.LogFormatter = (*zeroLogFormatter)(nil)
)
type zeroLogFormatter struct {
logger *zerolog.Logger
NoColor bool
}
type zeroLogEntry struct {
log *zerolog.Logger
request *http.Request
buf *bytes.Buffer
useColor bool
}
func New(l *zerolog.Logger) middleware.LogFormatter {
fi, err := os.Stdout.Stat()
if err == nil {
m := os.ModeDevice | os.ModeCharDevice
isTTY = fi.Mode()&m == m
}
color := true
if runtime.GOOS == "windows" {
color = false
}
return &zeroLogFormatter{
logger: l,
NoColor: !color,
}
}
func (z *zeroLogEntry) Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{}) {
switch {
case status < 200:
colorWrite(z.buf, z.useColor, bBlue, "%03d", status)
case status < 300:
colorWrite(z.buf, z.useColor, bGreen, "%03d", status)
case status < 400:
colorWrite(z.buf, z.useColor, bCyan, "%03d", status)
case status < 500:
colorWrite(z.buf, z.useColor, bYellow, "%03d", status)
default:
colorWrite(z.buf, z.useColor, bRed, "%03d", status)
}
colorWrite(z.buf, z.useColor, bBlue, " %dB", bytes)
z.buf.WriteString(" in ")
if elapsed < 500*time.Millisecond {
colorWrite(z.buf, z.useColor, nGreen, "%s", elapsed)
} else if elapsed < 5*time.Second {
colorWrite(z.buf, z.useColor, nYellow, "%s", elapsed)
} else {
colorWrite(z.buf, z.useColor, nRed, "%s", elapsed)
}
z.log.Info().Msg(z.buf.String())
}
func (z *zeroLogEntry) Panic(v interface{}, stack []byte) {
z.log.Info().Msgf("request failed: %+v", v)
}
func (l zeroLogFormatter) NewLogEntry(r *http.Request) middleware.LogEntry {
useColor := !l.NoColor
entry := &zeroLogEntry{
log: l.logger,
request: r,
buf: &bytes.Buffer{},
useColor: useColor,
}
colorWrite(entry.buf, useColor, nCyan, "\"")
colorWrite(entry.buf, useColor, bMagenta, "%s ", r.Method)
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
colorWrite(entry.buf, useColor, nCyan, "%s://%s%s %s\" ", scheme, r.Host, r.RequestURI, r.Proto)
entry.buf.WriteString("from ")
entry.buf.WriteString(r.RemoteAddr)
entry.buf.WriteString(" - ")
return entry
}
func colorWrite(w io.Writer, useColor bool, color []byte, s string, args ...interface{}) {
if isTTY && useColor {
w.Write(color)
}
fmt.Fprintf(w, s, args...)
if isTTY && useColor {
w.Write(reset)
}
}