/
loggingwriter.go
56 lines (46 loc) · 1010 Bytes
/
loggingwriter.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
package logging
import (
"bufio"
"fmt"
"net"
"net/http"
)
type LoggingWriter struct {
writer http.ResponseWriter
bytes int64
code int
}
func NewLoggingWriter(writer http.ResponseWriter) *LoggingWriter {
return &LoggingWriter{writer: writer}
}
func (lw *LoggingWriter) Write(data []byte) (count int, err error) {
count, err = lw.writer.Write(data)
lw.bytes += int64(count)
return
}
func (lw *LoggingWriter) WriteHeader(code int) {
lw.writer.WriteHeader(code)
if code == 0 {
code = 200
}
lw.code = code
}
func (lw *LoggingWriter) Header() http.Header {
return lw.writer.Header()
}
func (lw *LoggingWriter) Flush() {
lw.writer.(http.Flusher).Flush()
}
func (lw *LoggingWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hij, ok := lw.writer.(http.Hijacker)
if ok {
return hij.Hijack()
}
return nil, nil, fmt.Errorf("could not hijack connection")
}
func (lw *LoggingWriter) GetBytes() int64 {
return lw.bytes
}
func (lw *LoggingWriter) GetCode() int {
return lw.code
}