-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdlib_logger.go
62 lines (54 loc) · 1.16 KB
/
stdlib_logger.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
package middlelogger
import (
"log"
"net/http"
"runtime/debug"
"time"
)
// StdLibLogger is a simple logger that logs all requests using the stdlib's
// log package. It implements Logger, PanicLogger and the SlowRequestLogger
// with a fixed cutoff time of 1 second.
//
// The logging format as ad-hoc.
type StdLibLogger struct{}
// LogRequest is part of the Logger interface.
func (l StdLibLogger) LogRequest(ld LogData) {
log.Printf(
"%s %s %d %s %d",
ld.R.Method,
ld.R.RequestURI,
ld.Status,
ld.TotalTime,
ld.BytesWritten,
)
}
// LogPanic is part of the PanicLogger interface.
func (l StdLibLogger) LogPanic(ld LogData, err interface{}) {
log.Printf(
"%s %s %d %s %d (PANIC %v)",
ld.R.Method,
ld.R.RequestURI,
ld.Status,
ld.TotalTime,
ld.BytesWritten,
err,
)
log.Printf(string(debug.Stack()))
}
func (l StdLibLogger) Cutoff(*http.Request) time.Duration {
return time.Second
}
func (l StdLibLogger) MultipleLogs(*http.Request) bool {
return true
}
func (l StdLibLogger) LogSlowRequest(ld LogData, i int) {
log.Printf(
"%s %s %d %s %d (slow %d)",
ld.R.Method,
ld.R.RequestURI,
ld.Status,
ld.TotalTime,
ld.BytesWritten,
i,
)
}