-
Notifications
You must be signed in to change notification settings - Fork 51
/
json_output_writer.go
50 lines (39 loc) · 741 Bytes
/
json_output_writer.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
package main
import (
"encoding/json"
"io"
)
type jsonOutputWriter struct {
stream string
node string
host string
output io.Writer
}
func (writer *jsonOutputWriter) Write(data []byte) (int, error) {
if len(data) == 0 {
return 0, nil
}
message := map[string]interface{}{
"stream": writer.stream,
}
if writer.node == "" {
message["node"] = nil
} else {
message["node"] = writer.node
}
if writer.host == "" {
message["host"] = nil
} else {
message["host"] = writer.host
}
message["body"] = string(data)
jsonMessage, err := json.Marshal(message)
if err != nil {
return 0, err
}
_, err = writer.output.Write(append(jsonMessage, '\n'))
if err != nil {
return 0, err
}
return len(data), nil
}