forked from morita-kuma/golog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logevent_json.go
55 lines (43 loc) · 1.04 KB
/
logevent_json.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
package golog
import (
"fmt"
"os"
"encoding/json"
)
type EventData interface {
}
type JsonLogEvent struct {
event EventData
}
// Encode is implementation of LogEvent.Encode
func (jsonLogEvent *JsonLogEvent) Encode(data *LogEventMetadata) []byte {
if data == nil {
// encode json
encoded, err := json.Marshal(jsonLogEvent.event)
if err != nil {
fmt.Fprintf(os.Stdout, err.Error())
}
return encoded
} else {
eventData := struct {
EventData
LogLevel string `json:"logLevel,omitempty"`
Time string `json:"timestamp,omitempty"`
SourceLine string `json:"sourceLine,omitempty"`
SourceFile string `json:"sourceFile,omitempty"`
LoggerName string `json:"loggerName,omitempty"`
}{
EventData: jsonLogEvent.event,
LogLevel: data.GetLogLevel(),
Time: data.GetTime(),
SourceLine: data.GetSourceLine(),
SourceFile: data.GetSourceFile(),
LoggerName: data.GetLoggerName(),
}
encoded, err := json.Marshal(eventData)
if err != nil {
fmt.Fprint(os.Stdout, err.Error())
}
return encoded
}
}