feat(eventlog) entry
License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
Brian Tiger Chow committed
Nov 16, 2014 at 05:04 UTC
43f74c82ac112513234edf15860ed9a16366bd1a
1 file changed
+40
-6
util/eventlog/log.go
+40
-6
@@ -64,27 +64,61 @@ func Logger(system string) EventLogger {
64
type eventLogger struct {
65
*logging.Logger
66
system string
67
+ // TODO add log-level
68
}
69
70
func (el *eventLogger) Event(ctx context.Context, event string, metadata ...Loggable) {
71
72
+ // Collect loggables for later logging
73
+ var loggables []Loggable
74
+
75
// get any existing metadata from the context
76
existing, err := MetadataFromContext(ctx)
77
if err != nil {
78
existing = Metadata{}
79
}
80
+ loggables = append(loggables, existing)
81
77
- // accumulate metadata
78
- accum := existing
82
for _, datum := range metadata {
80
- accum = DeepMerge(accum, datum.Loggable())
83
+ loggables = append(loggables, datum)
84
+ }
85
+
86
+ e := entry{
87
+ loggables: loggables,
88
+ system: el.system,
89
+ event: event,
90
+ }
91
+
92
+ e.Log() // TODO replace this when leveled-logs have been implemented
93
+}
94
+
95
+type entry struct {
96
+ loggables []Loggable
97
+ system string
98
+ event string
99
+}
100
+
101
+// Log logs the event unconditionally (regardless of log level)
102
+// TODO add support for leveled-logs once we decide which levels we want
103
+// for our structured logs
104
+func (e *entry) Log() {
105
+ e.log()
106
+}
107
+
108
+// log is a private method invoked by the public Log, Info, Error methods
109
+func (e *entry) log() {
110
+ // accumulate metadata
111
+ accum := Metadata{}
112
+ for _, loggable := range e.loggables {
113
+ accum = DeepMerge(accum, loggable.Loggable())
114
}
115
116
// apply final attributes to reserved keys
84
- accum["event"] = event
85
- accum["system"] = el.system
117
+ // TODO accum["level"] = level
118
+ accum["event"] = e.event
119
+ accum["system"] = e.system
120
accum["time"] = util.FormatRFC3339(time.Now())
121
122
// TODO roll our own event logger
89
- logrus.WithFields(map[string]interface{}(accum)).Info(event)
123
+ logrus.WithFields(map[string]interface{}(accum)).Info(e.event)
124
}