/
logging.go
executable file
·59 lines (52 loc) · 1.56 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
/**
Copyright (c) 2016 The ConnectorDB Contributors
Licensed under the MIT license.
**/
package webcore
import (
"net/http"
"strings"
"time"
log "github.com/Sirupsen/logrus"
)
//Log levels supported by LogRequest
const (
DEBUG = iota
INFO = iota
WARNING = iota
ERROR = iota
)
//GetRequestLogger returns a logrus log entry which has fields prepopulated for the request
func GetRequestLogger(request *http.Request, opname string) *log.Entry {
//Since an important use case is behind nginx, the following rule is followed:
//localhost address is not logged if real-ip header exists (since it is from localhost)
//if real-ip header exists, faddr=address (forwardedAddress) is logged
//In essence, if behind nginx, there is no need for the addr=blah
fields := log.Fields{"addr": request.RemoteAddr, "uri": request.URL.Path, "op": opname}
if realIP := request.Header.Get("X-Real-IP"); realIP != "" {
fields["faddr"] = realIP
if strings.HasPrefix(request.RemoteAddr, "127.0.0.1") || strings.HasPrefix(request.RemoteAddr, "::1") {
delete(fields, "addr")
}
}
return log.WithFields(fields)
}
// LogRequest writes a log message given the log entry to use, a log level, optional text, and the query duration
func LogRequest(l *log.Entry, loglevel int, txt string, tdiff time.Duration) {
//Set up how the log message is printed for this query
if txt == "" {
txt = tdiff.String()
} else {
txt += " - " + tdiff.String()
}
switch loglevel {
case DEBUG:
l.Debugln(txt)
case INFO:
l.Infoln(txt)
case WARNING:
l.Warningln(txt)
case ERROR:
l.Errorln(txt)
}
}