From 348bace2692cddf1f24a90f452348b1eecda6400 Mon Sep 17 00:00:00 2001 From: Dave Clendenan Date: Tue, 29 Nov 2016 09:35:34 -0800 Subject: [PATCH] doc updates, and relabel ReportMethod in the Logrus context it's the caller, so use that internally. Label stays as 'method' since in the context of the log event that seems more correct. --- README.md | 2 +- entry.go | 6 +++--- exported.go | 14 ++++++-------- formatter.go | 4 ++-- json_formatter.go | 8 ++++---- json_formatter_test.go | 16 ++++++++-------- logger.go | 4 ++-- logrus_test.go | 14 +++++++------- text_formatter.go | 8 ++++---- 9 files changed, 37 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 57f15e4..25cc2c5 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ To ensure this behaviour even if a TTY is attached, set your formatter as follow If you wish to add the calling method as a field, instruct the logger via: ``` -log.SetReportMethod(true) +log.SetReportCaller(true) ``` This adds the caller as 'method' like so: diff --git a/entry.go b/entry.go index b7f4e9c..7949e1f 100644 --- a/entry.go +++ b/entry.go @@ -46,7 +46,7 @@ type Entry struct { Level Level // Calling method, with package name - Method string + Caller string // Message passed to Debug, Info, Warn, Error, Fatal or Panic Message string @@ -135,8 +135,8 @@ func (entry Entry) log(level Level, msg string) { entry.Time = time.Now() entry.Level = level entry.Message = msg - if ReportMethod() { - entry.Method = getCaller() + if ReportCaller() { + entry.Caller = getCaller() } if err := entry.Logger.Hooks.Fire(level, &entry); err != nil { entry.Logger.mu.Lock() diff --git a/exported.go b/exported.go index 7a02d7c..356d4a9 100644 --- a/exported.go +++ b/exported.go @@ -27,20 +27,18 @@ func SetFormatter(formatter Formatter) { std.Formatter = formatter } -// SetReportMethod sets whether to include calling method and line as -// fields -func SetReportMethod(include bool) { +// SetReportCaller sets whether to include the calling method as a field +func SetReportCaller(include bool) { std.mu.Lock() defer std.mu.Unlock() - std.ReportMethod = include + std.ReportCaller = include } -// ReportMethod sets whether to include calling method and line as -// fields -func ReportMethod() bool { +// ReportCaller returns the 'include calling method' state +func ReportCaller() bool { std.mu.Lock() defer std.mu.Unlock() - return std.ReportMethod + return std.ReportCaller } // SetLevel sets the standard logger level. diff --git a/formatter.go b/formatter.go index c48e02f..9e94689 100644 --- a/formatter.go +++ b/formatter.go @@ -44,8 +44,8 @@ func prefixFieldClashes(data Fields) { data["fields.level"] = l } - // If ReportMethod is not set, 'method' will not conflict. - if ReportMethod() { + // If Reportmethod is not set, 'method' will not conflict. + if ReportCaller() { if l, ok := data["method"]; ok { data["fields.method"] = l } diff --git a/json_formatter.go b/json_formatter.go index a84cc15..2bb3a6a 100644 --- a/json_formatter.go +++ b/json_formatter.go @@ -12,7 +12,7 @@ const ( FieldKeyMsg = "msg" FieldKeyLevel = "level" FieldKeyTime = "time" - FieldKeyMethod = "method" + FieldKeyCaller = "method" ) func (f FieldMap) resolve(key fieldKey) string { @@ -34,7 +34,7 @@ type JSONFormatter struct { // FieldKeyTime: "@timestamp", // FieldKeyLevel: "@level", // FieldKeyMsg: "@message", - // FieldKeyMethod: "@caller", + // FieldKeyCaller: "@caller", // }, // } FieldMap FieldMap @@ -62,8 +62,8 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) { data[f.FieldMap.resolve(FieldKeyTime)] = entry.Time.Format(timestampFormat) data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String() - if ReportMethod() { - data[f.FieldMap.resolve(FieldKeyMethod)] = entry.Method + if ReportCaller() { + data[f.FieldMap.resolve(FieldKeyCaller)] = entry.Caller } serialized, err := json.Marshal(data) if err != nil { diff --git a/json_formatter_test.go b/json_formatter_test.go index dc09bfe..ab52095 100644 --- a/json_formatter_test.go +++ b/json_formatter_test.go @@ -170,8 +170,8 @@ func TestJSONTimeKey(t *testing.T) { } } -func TestFieldDoesNotClashWithMethod(t *testing.T) { - SetReportMethod(false) +func TestFieldDoesNotClashWithCaller(t *testing.T) { + SetReportCaller(false) formatter := &JSONFormatter{} b, err := formatter.Format(WithField("method", "howdy pardner")) @@ -186,12 +186,12 @@ func TestFieldDoesNotClashWithMethod(t *testing.T) { } if entry["method"] != "howdy pardner" { - t.Fatal("method field replaced when ReportMethod=false") + t.Fatal("method field replaced when ReportCaller=false") } } -func TestFieldClashWithMethod(t *testing.T) { - SetReportMethod(true) +func TestFieldClashWithCaller(t *testing.T) { + SetReportCaller(true) formatter := &JSONFormatter{} b, err := formatter.Format(WithField("method", "howdy pardner")) @@ -206,14 +206,14 @@ func TestFieldClashWithMethod(t *testing.T) { } if entry["fields.method"] != "howdy pardner" { - t.Fatalf("fields.method not set to original method field when ReportMethod=true (got '%s')", + t.Fatalf("fields.method not set to original method field when ReportCaller=true (got '%s')", entry["fields.method"]) } if entry["method"] != "" { // since we didn't actually log, it's set to the empty string - t.Fatalf("method not set as expected when ReportMethod=true (got '%s')", + t.Fatalf("method not set as expected when ReportCaller=true (got '%s')", entry["method"]) } - SetReportMethod(false) // return to default value + SetReportCaller(false) // return to default value } diff --git a/logger.go b/logger.go index 64967c4..ddab68a 100644 --- a/logger.go +++ b/logger.go @@ -24,7 +24,7 @@ type Logger struct { Formatter Formatter //Flag for whether to log caller info (off by default) - ReportMethod bool + ReportCaller bool // The logging level the logger should log at. This is typically (and defaults // to) `logrus.Info`, which allows Info(), Warn(), Error() and Fatal() to be @@ -75,7 +75,7 @@ func New() *Logger { Formatter: new(TextFormatter), Hooks: make(LevelHooks), Level: InfoLevel, - ReportMethod: false, + ReportCaller: false, } } diff --git a/logrus_test.go b/logrus_test.go index 2ba154e..760a24b 100644 --- a/logrus_test.go +++ b/logrus_test.go @@ -56,11 +56,11 @@ func LogAndAssertText(t *testing.T, log func(*Logger), assertions func(fields ma assertions(fields) } -// TestReportMethod verifies that when ReportMethod is set, the 'method' field +// TestReportCaller verifies that when ReportCaller is set, the 'method' field // is added, and when it is unset it is not set or modified -func TestReportMethod(t *testing.T) { +func TestReportCaller(t *testing.T) { LogAndAssertJSON(t, func(log *Logger) { - SetReportMethod(false) + SetReportCaller(false) log.Print("testNoCaller") }, func(fields Fields) { assert.Equal(t, fields["msg"], "testNoCaller") @@ -69,7 +69,7 @@ func TestReportMethod(t *testing.T) { }) LogAndAssertJSON(t, func(log *Logger) { - SetReportMethod(true) + SetReportCaller(true) log.Print("testWithCaller") }, func(fields Fields) { assert.Equal(t, fields["msg"], "testWithCaller") @@ -77,7 +77,7 @@ func TestReportMethod(t *testing.T) { assert.Equal(t, fields["method"], "testing.tRunner") }) - SetReportMethod(false) // return to default value + SetReportCaller(false) // return to default value } func TestPrint(t *testing.T) { @@ -269,7 +269,7 @@ func TestNestedLoggingReportsCorrectCaller(t *testing.T) { var buffer bytes.Buffer var fields Fields - SetReportMethod(true) + SetReportCaller(true) logger := New() logger.Out = &buffer logger.Formatter = new(JSONFormatter) @@ -311,7 +311,7 @@ func TestNestedLoggingReportsCorrectCaller(t *testing.T) { assert.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry") assert.Equal(t, "testing.tRunner", fields["method"]) - SetReportMethod(false) // return to default value + SetReportCaller(false) // return to default value } func TestConvertLevelToString(t *testing.T) { diff --git a/text_formatter.go b/text_formatter.go index 4b3cc62..b003451 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -88,8 +88,8 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { f.appendKeyValue(b, "time", entry.Time.Format(timestampFormat)) } f.appendKeyValue(b, "level", entry.Level.String()) - if ReportMethod() { - f.appendKeyValue(b, "method", entry.Method) + if ReportCaller() { + f.appendKeyValue(b, "method", entry.Caller) } if entry.Message != "" { f.appendKeyValue(b, "msg", entry.Message) @@ -119,8 +119,8 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin levelText := strings.ToUpper(entry.Level.String())[0:4] caller := "" - if ReportMethod() { - caller = fmt.Sprintf(" %s()", entry.Method) + if ReportCaller() { + caller = fmt.Sprintf(" %s()", entry.Caller) } if !f.FullTimestamp {