From 41879e9ec22cb2c66f8404bcddd5d06371956026 Mon Sep 17 00:00:00 2001 From: fangdingjun Date: Thu, 24 May 2018 15:55:47 +0800 Subject: [PATCH] adjust log entry format --- formatters/text_formatter.go | 44 ++++++++++++++++++++---------------- formatters/util_fileline.go | 24 +++++++++++--------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/formatters/text_formatter.go b/formatters/text_formatter.go index 7fe3bc6..5f0937d 100644 --- a/formatters/text_formatter.go +++ b/formatters/text_formatter.go @@ -62,39 +62,43 @@ func (f *TextFormatter) Format(level log.Level, msg string, logger *log.Logger) timeStr := time.Now().Format(f.TimeFormat) buf.WriteString(timeStr) - // level - buf.WriteByte(' ') - if f.isterm { - buf.WriteString(level.ColorString()) - } else { - buf.WriteString(level.String()) - } + /* + // host + buf.WriteByte(' ') + buf.Write(f.host) - // host - buf.WriteByte(' ') - buf.Write(f.host) + // name + buf.WriteByte(' ') + buf.Write(f.app) - // name - buf.WriteByte(' ') - buf.Write(f.app) - - // pid - buf.WriteByte(' ') - buf.Write(f.pid) + // pid + buf.WriteByte(' ') + buf.Write(f.pid) + */ // file, line file, line := FilelineCaller(4) - buf.WriteByte(' ') + buf.WriteString(" [") buf.WriteString(file) buf.WriteByte(':') buf.WriteString(strconv.Itoa(line)) + buf.WriteString("]") - // msg + // level buf.WriteByte(' ') + if f.isterm { + buf.WriteString(level.ColorString()) + } else { + buf.WriteString(level.String()) + } + // msg + buf.WriteString(": ") buf.WriteString(msg) // newline - buf.WriteByte('\n') + if msg[len(msg)-1] != '\n' { + buf.WriteByte('\n') + } return buf.Bytes() } diff --git a/formatters/util_fileline.go b/formatters/util_fileline.go index 4b4047d..ec3f864 100644 --- a/formatters/util_fileline.go +++ b/formatters/util_fileline.go @@ -1,6 +1,7 @@ package formatters import ( + "path" "runtime" "strings" ) @@ -17,19 +18,20 @@ func FilelineCaller(skip int) (file string, line int) { if strings.Contains(file, "go-log/") { continue } - - // file = pkg/file.go - n := 0 - for i := len(file) - 1; i > 0; i-- { - if file[i] == '/' { - n++ - if n >= 2 { - file = file[i+1:] - break + /* + // file = pkg/file.go + n := 0 + for i := len(file) - 1; i > 0; i-- { + if file[i] == '/' { + n++ + if n >= 2 { + file = file[i+1:] + break + } } } - } - return file, line + */ + return path.Base(file), line } return "???", 0