forked from smallstep/certificates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclf.go
79 lines (73 loc) · 1.93 KB
/
clf.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
package logging
import (
"bytes"
"fmt"
"strconv"
"time"
"github.com/sirupsen/logrus"
)
var clfFields = [...]string{
"request-id", "remote-address", "name", "user-id", "time", "duration", "method", "path", "protocol", "status", "size",
}
// CommonLogFormat implements the logrus.Formatter interface it writes logrus
// entries using a CLF format prepended by the request-id.
type CommonLogFormat struct{}
// Format implements the logrus.Formatter interface. It returns the given
// logrus entry as a CLF line with the following format:
//
// <request-id> <remote-address> <name> <user-id> <time> <duration> "<method> <path> <protocol>" <status> <size>
//
// If a field is not known, the hyphen symbol (-) will be used.
func (f *CommonLogFormat) Format(entry *logrus.Entry) ([]byte, error) {
data := make([]string, len(clfFields))
for i, name := range clfFields {
if v, ok := entry.Data[name]; ok {
switch v := v.(type) {
case error:
data[i] = v.Error()
case string:
if v == "" {
data[i] = "-"
} else {
data[i] = v
}
case time.Time:
data[i] = v.Format(time.RFC3339)
case time.Duration:
data[i] = strconv.FormatInt(int64(v/time.Millisecond), 10)
case int:
data[i] = strconv.FormatInt(int64(v), 10)
case int64:
data[i] = strconv.FormatInt(v, 10)
default:
data[i] = fmt.Sprintf("%v", v)
}
} else {
data[i] = "-"
}
}
var buf bytes.Buffer
buf.WriteString(data[0])
buf.WriteByte(' ')
buf.WriteString(data[1])
buf.WriteByte(' ')
buf.WriteString(data[2])
buf.WriteByte(' ')
buf.WriteString(data[3])
buf.WriteByte(' ')
buf.WriteString(data[4])
buf.WriteByte(' ')
buf.WriteString(data[5])
buf.WriteString(" \"")
buf.WriteString(data[6])
buf.WriteByte(' ')
buf.WriteString(data[7])
buf.WriteByte(' ')
buf.WriteString(data[8])
buf.WriteString("\" ")
buf.WriteString(data[9])
buf.WriteByte(' ')
buf.WriteString(data[10])
buf.WriteByte('\n')
return buf.Bytes(), nil
}