forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
112 lines (87 loc) · 2.07 KB
/
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
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
package main
import (
"fmt"
"net/http"
logrus "github.com/sirupsen/logrus"
)
func initLog() error {
logFormat := "pretty"
strEnvConfig(&logFormat, "IMGPROXY_LOG_FORMAT")
switch logFormat {
case "structured":
logrus.SetFormatter(&logStructuredFormatter{})
case "json":
logrus.SetFormatter(&logrus.JSONFormatter{})
default:
logrus.SetFormatter(newLogPrettyFormatter())
}
logLevel := "info"
strEnvConfig(&logLevel, "IMGPROXY_LOG_LEVEL")
levelLogLevel, err := logrus.ParseLevel(logLevel)
if err != nil {
levelLogLevel = logrus.InfoLevel
}
logrus.SetLevel(levelLogLevel)
if isSyslogEnabled() {
slHook, err := newSyslogHook()
if err != nil {
return fmt.Errorf("Unable to connect to syslog daemon: %s", err)
}
logrus.AddHook(slHook)
}
return nil
}
func logRequest(reqID string, r *http.Request) {
path := r.RequestURI
logrus.WithFields(logrus.Fields{
"request_id": reqID,
"method": r.Method,
}).Infof("Started %s", path)
}
func logResponse(reqID string, r *http.Request, status int, err *imgproxyError, imageURL *string, po *processingOptions) {
var level logrus.Level
switch {
case status >= 500:
level = logrus.ErrorLevel
case status >= 400:
level = logrus.WarnLevel
default:
level = logrus.InfoLevel
}
fields := logrus.Fields{
"request_id": reqID,
"method": r.Method,
"status": status,
}
if err != nil {
fields["error"] = err
if stack := err.FormatStack(); len(stack) > 0 {
fields["stack"] = stack
}
}
if imageURL != nil {
fields["image_url"] = *imageURL
}
if po != nil {
fields["processing_options"] = po
}
logrus.WithFields(fields).Logf(
level,
"Completed in %s %s", getTimerSince(r.Context()), r.RequestURI,
)
}
func logNotice(f string, args ...interface{}) {
logrus.Infof(f, args...)
}
func logWarning(f string, args ...interface{}) {
logrus.Warnf(f, args...)
}
func logError(f string, args ...interface{}) {
logrus.Errorf(f, args...)
}
func logFatal(f string, args ...interface{}) {
logrus.Fatalf(f, args...)
}
func logDebug(f string, args ...interface{}) {
logrus.Debugf(f, args...)
}