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.
This commit is contained in:
Dave Clendenan 2016-11-29 09:35:34 -08:00
parent 1e21450408
commit 348bace269
9 changed files with 37 additions and 39 deletions

View File

@ -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:

View File

@ -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()

View File

@ -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.

View File

@ -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
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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,
}
}

View File

@ -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) {

View File

@ -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 {