mirror of https://github.com/sirupsen/logrus.git
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:
parent
1e21450408
commit
348bace269
|
@ -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:
|
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:
|
This adds the caller as 'method' like so:
|
||||||
|
|
||||||
|
|
6
entry.go
6
entry.go
|
@ -46,7 +46,7 @@ type Entry struct {
|
||||||
Level Level
|
Level Level
|
||||||
|
|
||||||
// Calling method, with package name
|
// Calling method, with package name
|
||||||
Method string
|
Caller string
|
||||||
|
|
||||||
// Message passed to Debug, Info, Warn, Error, Fatal or Panic
|
// Message passed to Debug, Info, Warn, Error, Fatal or Panic
|
||||||
Message string
|
Message string
|
||||||
|
@ -135,8 +135,8 @@ func (entry Entry) log(level Level, msg string) {
|
||||||
entry.Time = time.Now()
|
entry.Time = time.Now()
|
||||||
entry.Level = level
|
entry.Level = level
|
||||||
entry.Message = msg
|
entry.Message = msg
|
||||||
if ReportMethod() {
|
if ReportCaller() {
|
||||||
entry.Method = getCaller()
|
entry.Caller = getCaller()
|
||||||
}
|
}
|
||||||
if err := entry.Logger.Hooks.Fire(level, &entry); err != nil {
|
if err := entry.Logger.Hooks.Fire(level, &entry); err != nil {
|
||||||
entry.Logger.mu.Lock()
|
entry.Logger.mu.Lock()
|
||||||
|
|
14
exported.go
14
exported.go
|
@ -27,20 +27,18 @@ func SetFormatter(formatter Formatter) {
|
||||||
std.Formatter = formatter
|
std.Formatter = formatter
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetReportMethod sets whether to include calling method and line as
|
// SetReportCaller sets whether to include the calling method as a field
|
||||||
// fields
|
func SetReportCaller(include bool) {
|
||||||
func SetReportMethod(include bool) {
|
|
||||||
std.mu.Lock()
|
std.mu.Lock()
|
||||||
defer std.mu.Unlock()
|
defer std.mu.Unlock()
|
||||||
std.ReportMethod = include
|
std.ReportCaller = include
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReportMethod sets whether to include calling method and line as
|
// ReportCaller returns the 'include calling method' state
|
||||||
// fields
|
func ReportCaller() bool {
|
||||||
func ReportMethod() bool {
|
|
||||||
std.mu.Lock()
|
std.mu.Lock()
|
||||||
defer std.mu.Unlock()
|
defer std.mu.Unlock()
|
||||||
return std.ReportMethod
|
return std.ReportCaller
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLevel sets the standard logger level.
|
// SetLevel sets the standard logger level.
|
||||||
|
|
|
@ -44,8 +44,8 @@ func prefixFieldClashes(data Fields) {
|
||||||
data["fields.level"] = l
|
data["fields.level"] = l
|
||||||
}
|
}
|
||||||
|
|
||||||
// If ReportMethod is not set, 'method' will not conflict.
|
// If Reportmethod is not set, 'method' will not conflict.
|
||||||
if ReportMethod() {
|
if ReportCaller() {
|
||||||
if l, ok := data["method"]; ok {
|
if l, ok := data["method"]; ok {
|
||||||
data["fields.method"] = l
|
data["fields.method"] = l
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ const (
|
||||||
FieldKeyMsg = "msg"
|
FieldKeyMsg = "msg"
|
||||||
FieldKeyLevel = "level"
|
FieldKeyLevel = "level"
|
||||||
FieldKeyTime = "time"
|
FieldKeyTime = "time"
|
||||||
FieldKeyMethod = "method"
|
FieldKeyCaller = "method"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (f FieldMap) resolve(key fieldKey) string {
|
func (f FieldMap) resolve(key fieldKey) string {
|
||||||
|
@ -34,7 +34,7 @@ type JSONFormatter struct {
|
||||||
// FieldKeyTime: "@timestamp",
|
// FieldKeyTime: "@timestamp",
|
||||||
// FieldKeyLevel: "@level",
|
// FieldKeyLevel: "@level",
|
||||||
// FieldKeyMsg: "@message",
|
// FieldKeyMsg: "@message",
|
||||||
// FieldKeyMethod: "@caller",
|
// FieldKeyCaller: "@caller",
|
||||||
// },
|
// },
|
||||||
// }
|
// }
|
||||||
FieldMap FieldMap
|
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(FieldKeyTime)] = entry.Time.Format(timestampFormat)
|
||||||
data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message
|
data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message
|
||||||
data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String()
|
data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String()
|
||||||
if ReportMethod() {
|
if ReportCaller() {
|
||||||
data[f.FieldMap.resolve(FieldKeyMethod)] = entry.Method
|
data[f.FieldMap.resolve(FieldKeyCaller)] = entry.Caller
|
||||||
}
|
}
|
||||||
serialized, err := json.Marshal(data)
|
serialized, err := json.Marshal(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -170,8 +170,8 @@ func TestJSONTimeKey(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFieldDoesNotClashWithMethod(t *testing.T) {
|
func TestFieldDoesNotClashWithCaller(t *testing.T) {
|
||||||
SetReportMethod(false)
|
SetReportCaller(false)
|
||||||
formatter := &JSONFormatter{}
|
formatter := &JSONFormatter{}
|
||||||
|
|
||||||
b, err := formatter.Format(WithField("method", "howdy pardner"))
|
b, err := formatter.Format(WithField("method", "howdy pardner"))
|
||||||
|
@ -186,12 +186,12 @@ func TestFieldDoesNotClashWithMethod(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if entry["method"] != "howdy pardner" {
|
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) {
|
func TestFieldClashWithCaller(t *testing.T) {
|
||||||
SetReportMethod(true)
|
SetReportCaller(true)
|
||||||
formatter := &JSONFormatter{}
|
formatter := &JSONFormatter{}
|
||||||
|
|
||||||
b, err := formatter.Format(WithField("method", "howdy pardner"))
|
b, err := formatter.Format(WithField("method", "howdy pardner"))
|
||||||
|
@ -206,14 +206,14 @@ func TestFieldClashWithMethod(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if entry["fields.method"] != "howdy pardner" {
|
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"])
|
entry["fields.method"])
|
||||||
}
|
}
|
||||||
|
|
||||||
if entry["method"] != "" { // since we didn't actually log, it's set to the empty string
|
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"])
|
entry["method"])
|
||||||
}
|
}
|
||||||
|
|
||||||
SetReportMethod(false) // return to default value
|
SetReportCaller(false) // return to default value
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ type Logger struct {
|
||||||
Formatter Formatter
|
Formatter Formatter
|
||||||
|
|
||||||
//Flag for whether to log caller info (off by default)
|
//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
|
// 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
|
// to) `logrus.Info`, which allows Info(), Warn(), Error() and Fatal() to be
|
||||||
|
@ -75,7 +75,7 @@ func New() *Logger {
|
||||||
Formatter: new(TextFormatter),
|
Formatter: new(TextFormatter),
|
||||||
Hooks: make(LevelHooks),
|
Hooks: make(LevelHooks),
|
||||||
Level: InfoLevel,
|
Level: InfoLevel,
|
||||||
ReportMethod: false,
|
ReportCaller: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,11 +56,11 @@ func LogAndAssertText(t *testing.T, log func(*Logger), assertions func(fields ma
|
||||||
assertions(fields)
|
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
|
// 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) {
|
LogAndAssertJSON(t, func(log *Logger) {
|
||||||
SetReportMethod(false)
|
SetReportCaller(false)
|
||||||
log.Print("testNoCaller")
|
log.Print("testNoCaller")
|
||||||
}, func(fields Fields) {
|
}, func(fields Fields) {
|
||||||
assert.Equal(t, fields["msg"], "testNoCaller")
|
assert.Equal(t, fields["msg"], "testNoCaller")
|
||||||
|
@ -69,7 +69,7 @@ func TestReportMethod(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
LogAndAssertJSON(t, func(log *Logger) {
|
LogAndAssertJSON(t, func(log *Logger) {
|
||||||
SetReportMethod(true)
|
SetReportCaller(true)
|
||||||
log.Print("testWithCaller")
|
log.Print("testWithCaller")
|
||||||
}, func(fields Fields) {
|
}, func(fields Fields) {
|
||||||
assert.Equal(t, fields["msg"], "testWithCaller")
|
assert.Equal(t, fields["msg"], "testWithCaller")
|
||||||
|
@ -77,7 +77,7 @@ func TestReportMethod(t *testing.T) {
|
||||||
assert.Equal(t, fields["method"], "testing.tRunner")
|
assert.Equal(t, fields["method"], "testing.tRunner")
|
||||||
})
|
})
|
||||||
|
|
||||||
SetReportMethod(false) // return to default value
|
SetReportCaller(false) // return to default value
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPrint(t *testing.T) {
|
func TestPrint(t *testing.T) {
|
||||||
|
@ -269,7 +269,7 @@ func TestNestedLoggingReportsCorrectCaller(t *testing.T) {
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
var fields Fields
|
var fields Fields
|
||||||
|
|
||||||
SetReportMethod(true)
|
SetReportCaller(true)
|
||||||
logger := New()
|
logger := New()
|
||||||
logger.Out = &buffer
|
logger.Out = &buffer
|
||||||
logger.Formatter = new(JSONFormatter)
|
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.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry")
|
||||||
assert.Equal(t, "testing.tRunner", fields["method"])
|
assert.Equal(t, "testing.tRunner", fields["method"])
|
||||||
|
|
||||||
SetReportMethod(false) // return to default value
|
SetReportCaller(false) // return to default value
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConvertLevelToString(t *testing.T) {
|
func TestConvertLevelToString(t *testing.T) {
|
||||||
|
|
|
@ -88,8 +88,8 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
|
||||||
f.appendKeyValue(b, "time", entry.Time.Format(timestampFormat))
|
f.appendKeyValue(b, "time", entry.Time.Format(timestampFormat))
|
||||||
}
|
}
|
||||||
f.appendKeyValue(b, "level", entry.Level.String())
|
f.appendKeyValue(b, "level", entry.Level.String())
|
||||||
if ReportMethod() {
|
if ReportCaller() {
|
||||||
f.appendKeyValue(b, "method", entry.Method)
|
f.appendKeyValue(b, "method", entry.Caller)
|
||||||
}
|
}
|
||||||
if entry.Message != "" {
|
if entry.Message != "" {
|
||||||
f.appendKeyValue(b, "msg", 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]
|
levelText := strings.ToUpper(entry.Level.String())[0:4]
|
||||||
|
|
||||||
caller := ""
|
caller := ""
|
||||||
if ReportMethod() {
|
if ReportCaller() {
|
||||||
caller = fmt.Sprintf(" %s()", entry.Method)
|
caller = fmt.Sprintf(" %s()", entry.Caller)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !f.FullTimestamp {
|
if !f.FullTimestamp {
|
||||||
|
|
Loading…
Reference in New Issue