-
Notifications
You must be signed in to change notification settings - Fork 3
/
log.go
43 lines (36 loc) · 919 Bytes
/
log.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
package main
import (
"io"
"log"
"net/http"
"os"
"github.com/whytheplatypus/flushable"
)
var (
accessLog = log.New(os.Stderr, "[access] ", log.LstdFlags)
registrationLog = log.New(os.Stderr, "[registration] ", log.LstdFlags)
routingLog = log.New(os.Stderr, "[routing] ", log.LstdFlags)
)
func configureLog(addr string) {
go func() {
r := http.NewServeMux()
r.Handle("/debug/access", logHandler(accessLog))
r.Handle("/debug/registration", logHandler(registrationLog))
r.Handle("/debug/routing", logHandler(routingLog))
if err := http.ListenAndServe(addr, r); err != nil {
log.Println(err)
}
}()
}
func logHandler(l *log.Logger) http.Handler {
m := &flushable.MultiFlusher{}
l.SetOutput(io.MultiWriter(m, l.Writer()))
return m
}
type lWriter struct {
*log.Logger
}
func (lw *lWriter) Write(p []byte) (n int, err error) {
err = lw.Output(2, string(p))
return len(p), err
}