adjust log entry format

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

@ -62,39 +62,43 @@ 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(' ') // host
if f.isterm { buf.WriteByte(' ')
buf.WriteString(level.ColorString()) buf.Write(f.host)
} else {
buf.WriteString(level.String())
}
// host // name
buf.WriteByte(' ') buf.WriteByte(' ')
buf.Write(f.host) buf.Write(f.app)
// name // pid
buf.WriteByte(' ') buf.WriteByte(' ')
buf.Write(f.app) buf.Write(f.pid)
*/
// pid
buf.WriteByte(' ')
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
buf.WriteByte('\n') if msg[len(msg)-1] != '\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,19 +18,20 @@ 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-- {
if file[i] == '/' { if file[i] == '/' {
n++ n++
if n >= 2 { if n >= 2 {
file = file[i+1:] file = file[i+1:]
break break
}
} }
} }
} */
return file, line return path.Base(file), line
} }
return "???", 0 return "???", 0

Loading…
Cancel
Save