Merge pull request #944 from hrxu01/remove_field_if_val_is_empty_for_func_file_filed

remove field if val is empty string for func and file field
This commit is contained in:
David Bariod 2019-04-02 09:30:20 +02:00 committed by GitHub
commit 717f2ccd7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 6 deletions

View File

@ -73,9 +73,9 @@ type TextFormatter struct {
FieldMap FieldMap FieldMap FieldMap
// CallerPrettyfier can be set by the user to modify the content // CallerPrettyfier can be set by the user to modify the content
// of the function and file keys in the json data when ReportCaller is // of the function and file keys in the data when ReportCaller is
// activated. If any of the returned value is the empty string the // activated. If any of the returned value is the empty string the
// corresponding key will be removed from json fields. // corresponding key will be removed from fields.
CallerPrettyfier func(*runtime.Frame) (function string, file string) CallerPrettyfier func(*runtime.Frame) (function string, file string)
terminalInitOnce sync.Once terminalInitOnce sync.Once
@ -133,14 +133,19 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyLogrusError)) fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyLogrusError))
} }
if entry.HasCaller() { if entry.HasCaller() {
fixedKeys = append(fixedKeys,
f.FieldMap.resolve(FieldKeyFunc), f.FieldMap.resolve(FieldKeyFile))
if f.CallerPrettyfier != nil { if f.CallerPrettyfier != nil {
funcVal, fileVal = f.CallerPrettyfier(entry.Caller) funcVal, fileVal = f.CallerPrettyfier(entry.Caller)
} else { } else {
funcVal = entry.Caller.Function funcVal = entry.Caller.Function
fileVal = fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line) fileVal = fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
} }
if funcVal != "" {
fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyFunc))
}
if fileVal != "" {
fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyFile))
}
} }
if !f.DisableSorting { if !f.DisableSorting {
@ -225,7 +230,6 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin
entry.Message = strings.TrimSuffix(entry.Message, "\n") entry.Message = strings.TrimSuffix(entry.Message, "\n")
caller := "" caller := ""
if entry.HasCaller() { if entry.HasCaller() {
funcVal := fmt.Sprintf("%s()", entry.Caller.Function) funcVal := fmt.Sprintf("%s()", entry.Caller.Function)
fileVal := fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line) fileVal := fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
@ -233,8 +237,15 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin
if f.CallerPrettyfier != nil { if f.CallerPrettyfier != nil {
funcVal, fileVal = f.CallerPrettyfier(entry.Caller) funcVal, fileVal = f.CallerPrettyfier(entry.Caller)
} }
if fileVal == "" {
caller = funcVal
} else if funcVal == "" {
caller = fileVal
} else {
caller = fileVal + " " + funcVal caller = fileVal + " " + funcVal
} }
}
if f.DisableTimestamp { if f.DisableTimestamp {
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m%s %-44s ", levelColor, levelText, caller, entry.Message) fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m%s %-44s ", levelColor, levelText, caller, entry.Message)