adjust log entry format

remotes/origin/HEAD
fangdingjun 7 years ago
parent e36040bfce
commit 41879e9ec2

@ -62,14 +62,7 @@ func (f *TextFormatter) Format(level log.Level, msg string, logger *log.Logger)
timeStr := time.Now().Format(f.TimeFormat) timeStr := time.Now().Format(f.TimeFormat)
buf.WriteString(timeStr) buf.WriteString(timeStr)
// level /*
buf.WriteByte(' ')
if f.isterm {
buf.WriteString(level.ColorString())
} else {
buf.WriteString(level.String())
}
// host // host
buf.WriteByte(' ') buf.WriteByte(' ')
buf.Write(f.host) buf.Write(f.host)
@ -81,20 +74,31 @@ func (f *TextFormatter) Format(level log.Level, msg string, logger *log.Logger)
// pid // pid
buf.WriteByte(' ') buf.WriteByte(' ')
buf.Write(f.pid) buf.Write(f.pid)
*/
// file, line // file, line
file, line := FilelineCaller(4) file, line := FilelineCaller(4)
buf.WriteByte(' ') buf.WriteString(" [")
buf.WriteString(file) buf.WriteString(file)
buf.WriteByte(':') buf.WriteByte(':')
buf.WriteString(strconv.Itoa(line)) buf.WriteString(strconv.Itoa(line))
buf.WriteString("]")
// msg // level
buf.WriteByte(' ') buf.WriteByte(' ')
if f.isterm {
buf.WriteString(level.ColorString())
} else {
buf.WriteString(level.String())
}
// msg
buf.WriteString(": ")
buf.WriteString(msg) buf.WriteString(msg)
// newline // newline
if msg[len(msg)-1] != '\n' {
buf.WriteByte('\n') buf.WriteByte('\n')
}
return buf.Bytes() return buf.Bytes()
} }

@ -1,6 +1,7 @@
package formatters package formatters
import ( import (
"path"
"runtime" "runtime"
"strings" "strings"
) )
@ -17,7 +18,7 @@ func FilelineCaller(skip int) (file string, line int) {
if strings.Contains(file, "go-log/") { if strings.Contains(file, "go-log/") {
continue continue
} }
/*
// file = pkg/file.go // file = pkg/file.go
n := 0 n := 0
for i := len(file) - 1; i > 0; i-- { for i := len(file) - 1; i > 0; i-- {
@ -29,7 +30,8 @@ func FilelineCaller(skip int) (file string, line int) {
} }
} }
} }
return file, line */
return path.Base(file), line
} }
return "???", 0 return "???", 0

Loading…
Cancel
Save