forked from thatguystone/cog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_logstash.go
46 lines (37 loc) · 930 Bytes
/
format_logstash.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
package clog
import (
"encoding/json"
"time"
"github.com/iheartradio/cog/cio/eio"
)
// LogstashFormat formats messages as JSON, with special fields for logstash
type LogstashFormat struct{}
type logstashEntry struct {
Entry
Time time.Time `json:"@timestamp"`
Host string `json:"@host"`
Version int `json:"@version"`
// Hide these fields
OmitTime *struct{} `json:"time,omitempty"`
OmitHost *struct{} `json:"host,omitempty"`
}
func init() {
RegisterFormatter("logstash",
func(args eio.Args) (Formatter, error) {
return LogstashFormat{}, nil
})
}
// FormatEntry implements Formatter.FormatEntry
func (LogstashFormat) FormatEntry(e Entry) ([]byte, error) {
le := logstashEntry{
Entry: e,
Time: e.Time,
Host: Hostname(),
Version: 1,
}
return json.Marshal(le)
}
// MimeType implements Formatter.MimeType
func (LogstashFormat) MimeType() string {
return "application/json"
}