-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter_gelf.go
172 lines (145 loc) · 3.65 KB
/
formatter_gelf.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package gelf
import (
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/kardianos/osext"
log "github.com/kdar/factorlog"
)
// Extra is a type you wrap around data to be added
// as extra fields to JSON.
type Extra map[string]interface{}
// GLEFFormatter provides a formatter that outputs GELF v1.1 data.
// It will also encode extra information such as an http.Request.
type GELFFormatter struct {
hostname string
appname string
tmp []byte
}
// NewGELFFormatter returns a new GELFFormatter
func NewGELFFormatter() *GELFFormatter {
f := &GELFFormatter{tmp: make([]byte, 64)}
var err error
if f.hostname, err = os.Hostname(); err != nil {
f.hostname = "unknown"
}
if f.appname, err = osext.Executable(); err == nil {
f.appname = filepath.Base(f.appname)
} else {
f.appname = "app"
}
return f
}
func New() *GELFFormatter {
return NewGELFFormatter()
}
// ShouldRuntimeCaller will always return true.
func (f *GELFFormatter) ShouldRuntimeCaller() bool {
return true
}
// gelf is the strucuture of GELF 1.1 messages.
type gelf struct {
Version string `json:"version"`
Host string `json:"host"`
Short string `json:"short_message"`
Full string `json:"full_message,omitempty"`
TimeUnix int64 `json:"timestamp"`
Level int32 `json:"level,ompitempty"`
Extra map[string]interface{} `json:"-"`
}
type gelfBase gelf
// MarshalJSON is a special marshaller for marshalling the gelf
// structure and the extra data.
func (g *gelf) MarshalJSON() ([]byte, error) {
var err error
var b, eb []byte
extra := g.Extra
b, err = json.Marshal((*gelfBase)(g))
g.Extra = extra
if err != nil {
return nil, err
}
if len(extra) == 0 {
return b, nil
}
if eb, err = json.Marshal(extra); err != nil {
return nil, err
}
b[len(b)-1] = ','
return append(b, eb[1:len(eb)]...), nil
}
// Format formats a LogContext into the GELF format.
func (f *GELFFormatter) Format(context log.LogContext) []byte {
file := context.File
if len(file) == 0 {
file = "???"
} else {
slash := len(file) - 1
for ; slash >= 0; slash-- {
if file[slash] == filepath.Separator {
break
}
}
if slash >= 0 {
file = file[slash+1:]
}
}
e := Extra{
"_file": file,
"_line": context.Line,
"_severity": log.LcSeverityStrings[log.SeverityToIndex(context.Severity)],
"_facility": f.appname + "." + context.Function,
}
var args []interface{}
for _, v := range context.Args {
switch t := v.(type) {
case Extra:
for key, value := range t {
e[key] = value
}
case *http.Request:
header := make(map[string]interface{})
// Copy header
for key, value := range t.Header {
// Don't copy auth. Don't want this in logs.
// Note: If you add more auth schemes then you have to update
// this code to remove those too.
if strings.EqualFold(key, "authorization") {
continue
}
header[key] = value
}
e["_request_method"] = t.Method
// We have to use RequestURI because URL may be
// modified by routes.
e["_request_url"] = t.RequestURI //t.URL.String(),
e["_request_host"] = t.Host
e["_request_remote_addr"] = t.RemoteAddr
e["_request_header"] = header
default:
args = append(args, v)
}
}
message := ""
if context.Format != nil {
message = fmt.Sprintf(*context.Format, args...)
} else {
message = fmt.Sprint(args...)
}
gf := gelf{
Version: "1.1",
Host: f.hostname,
Short: message,
//Full: message,
TimeUnix: time.Now().Unix(),
Level: 6, // info
Extra: e,
}
buf, _ := json.Marshal(&gf)
buf = append(buf, '\n')
return buf
}