forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
90 lines (71 loc) · 1.83 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
package main
import (
"fmt"
"log"
"log/syslog"
"net/http"
)
const (
logRequestFmt = "[%s] %s: %s"
logRequestSyslogFmt = "REQUEST [%s] %s: %s"
logResponseFmt = "[%s] |\033[7;%dm %d \033[0m| %s"
logResponseSyslogFmt = "RESPONSE [%s] | %d | %s"
logWarningFmt = "\033[1;33m[WARNING]\033[0m %s"
logWarningSyslogFmt = "WARNING %s"
logFatalSyslogFmt = "FATAL %s"
)
func logRequest(reqID string, r *http.Request) {
path := r.URL.RequestURI()
log.Printf(logRequestFmt, reqID, r.Method, path)
if syslogWriter != nil {
syslogWriter.Notice(fmt.Sprintf(logRequestSyslogFmt, reqID, r.Method, path))
}
}
func logResponse(reqID string, status int, msg string) {
var color int
if status >= 500 {
color = 31
} else if status >= 400 {
color = 33
} else {
color = 32
}
log.Printf(logResponseFmt, reqID, color, status, msg)
if syslogWriter != nil {
msg := fmt.Sprintf(logResponseSyslogFmt, reqID, status, msg)
if status >= 500 {
if syslogLevel >= syslog.LOG_ERR {
syslogWriter.Err(msg)
}
} else if status >= 400 {
if syslogLevel >= syslog.LOG_WARNING {
syslogWriter.Warning(msg)
}
} else {
if syslogLevel >= syslog.LOG_NOTICE {
syslogWriter.Notice(msg)
}
}
}
}
func logNotice(f string, args ...interface{}) {
msg := fmt.Sprintf(f, args...)
log.Print(msg)
if syslogWriter != nil && syslogLevel >= syslog.LOG_NOTICE {
syslogWriter.Notice(msg)
}
}
func logWarning(f string, args ...interface{}) {
msg := fmt.Sprintf(f, args...)
log.Printf(logWarningFmt, msg)
if syslogWriter != nil && syslogLevel >= syslog.LOG_WARNING {
syslogWriter.Warning(fmt.Sprintf(logWarningSyslogFmt, msg))
}
}
func logFatal(f string, args ...interface{}) {
msg := fmt.Sprintf(f, args...)
if syslogWriter != nil {
syslogWriter.Crit(fmt.Sprintf(logFatalSyslogFmt, msg))
}
log.Fatal(msg)
}