deps(eventlog) disable logrus' mutation of event keys
now, logrus does not automatically inject time, msg, level metadata License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
Brian Tiger Chow committed
Nov 16, 2014 at 06:00 UTC
2d6b10f60540a73ba3f50862cc1452d8362d05d8
7 files changed
+42
-5
Godeps/Godeps.json
+2
-2
@@ -133,8 +133,8 @@
133
},
134
{
135
"ImportPath": "github.com/maybebtc/logrus",
136
- "Comment": "v0.6.0-5-gf92b795",
137
- "Rev": "f92b7950b372b1db80bd3527e4d40e42555fe6c2"
136
+ "Comment": "v0.6.0-7-g51cc99e",
137
+ "Rev": "51cc99e4b07ec5516233a8370c28b5eedd096b6b"
138
},
139
{
140
"ImportPath": "github.com/mitchellh/go-homedir",
Godeps/_workspace/src/github.com/maybebtc/logrus/entry.go
+8
-3
@@ -71,9 +71,14 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
71
}
72
73
func (entry *Entry) log(level Level, msg string) {
74
- entry.Time = time.Now()
75
- entry.Level = level
76
- entry.Message = msg
74
+
75
+ entry.Logger.mu.Lock()
76
+ if entry.Logger.WriteFields {
77
+ entry.Time = time.Now()
78
+ entry.Level = level
79
+ entry.Message = msg
80
+ }
81
+ entry.Logger.mu.Unlock()
82
83
if err := entry.Logger.Hooks.Fire(level, entry); err != nil {
84
entry.Logger.mu.Lock()
Godeps/_workspace/src/github.com/maybebtc/logrus/entry_test.go
+2
@@ -26,6 +26,7 @@ func TestEntryPanicln(t *testing.T) {
26
27
logger := New()
28
logger.Out = &bytes.Buffer{}
29
+ logger.WriteFields = true
30
entry := NewEntry(logger)
31
entry.WithField("err", errBoom).Panicln("kaboom")
32
}
@@ -48,6 +49,7 @@ func TestEntryPanicf(t *testing.T) {
49
50
logger := New()
51
logger.Out = &bytes.Buffer{}
52
+ logger.WriteFields = true
53
entry := NewEntry(logger)
54
entry.WithField("err", errBoom).Panicf("kaboom %v", true)
55
}
Godeps/_workspace/src/github.com/maybebtc/logrus/exported.go
+8
@@ -56,6 +56,14 @@ func WithFields(fields Fields) *Entry {
56
return std.WithFields(fields)
57
}
58
59
+// WriteFields gives the Logger permission to add time, level, msg info to
60
+// Entries
61
+func WriteFields(b bool) {
62
+ std.mu.Lock()
63
+ defer std.mu.Unlock()
64
+ std.WriteFields = b
65
+}
66
+
67
// Debug logs a message at level Debug on the standard logger.
68
func Debug(args ...interface{}) {
69
std.Debug(args...)
Godeps/_workspace/src/github.com/maybebtc/logrus/logger.go
+5
@@ -26,6 +26,11 @@ type Logger struct {
26
// to) `logrus.Info`, which allows Info(), Warn(), Error() and Fatal() to be
27
// logged. `logrus.Debug` is useful in
28
Level Level
29
+
30
+ // WriteFields permits the logger to add time, msg, level data to
31
+ // user-provided Fields
32
+ WriteFields bool
33
+
34
// Used to sync writing to the log.
35
mu sync.Mutex
36
}
Godeps/_workspace/src/github.com/maybebtc/logrus/logrus_test.go
+1
@@ -17,6 +17,7 @@ func LogAndAssertJSON(t *testing.T, log func(*Logger), assertions func(fields Fi
17
logger := New()
18
logger.Out = &buffer
19
logger.Formatter = new(JSONFormatter)
20
+ logger.WriteFields = true
21
22
log(logger)
23
Godeps/_workspace/src/github.com/maybebtc/logrus/polite_json_formatter.go
new
+16
@@ -0,0 +1,16 @@
1
+package logrus
2
+
3
+import (
4
+ "encoding/json"
5
+ "fmt"
6
+)
7
+
8
+type PoliteJSONFormatter struct{}
9
+
10
+func (f *PoliteJSONFormatter) Format(entry *Entry) ([]byte, error) {
11
+ serialized, err := json.Marshal(entry.Data)
12
+ if err != nil {
13
+ return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err)
14
+ }
15
+ return append(serialized, '\n'), nil
16
+}