Refactor logger

This commit is contained in:
Jinzhu 2020-03-14 19:00:41 +08:00
parent f7f633590f
commit 477efab8cd
1 changed files with 48 additions and 41 deletions

View File

@ -18,7 +18,8 @@ const (
Magenta = "\033[35m" Magenta = "\033[35m"
Cyan = "\033[36m" Cyan = "\033[36m"
White = "\033[37m" White = "\033[37m"
Redbold = "\033[31;1m" MagentaBold = "\033[35;1m"
RedBold = "\033[31;1m"
YellowBold = "\033[33;1m" YellowBold = "\033[33;1m"
) )
@ -59,37 +60,40 @@ var Default = New(log.New(os.Stdout, "\r\n", log.LstdFlags), Config{
func New(writer Writer, config Config) Interface { func New(writer Writer, config Config) Interface {
var ( var (
infoPrefix = "%s\n[info] " infoStr = "%s\n[info] "
warnPrefix = "%s\n[warn] " warnStr = "%s\n[warn] "
errPrefix = "%s\n[error] " errStr = "%s\n[error] "
tracePrefix = "%s\n[%v] [rows:%d] %s" traceStr = "%s\n[%v] [rows:%d] %s"
traceErrPrefix = "%s\n[%v] [rows:%d] %s" traceWarnStr = "%s\n[%v] [rows:%d] %s"
traceErrStr = "%s %s\n[%v] [rows:%d] %s"
) )
if config.Colorful { if config.Colorful {
infoPrefix = Green + "%s\n" + Reset + Green + "[info] " + Reset infoStr = Green + "%s\n" + Reset + Green + "[info] " + Reset
warnPrefix = Blue + "%s\n" + Reset + Magenta + "[warn] " + Reset warnStr = Blue + "%s\n" + Reset + Magenta + "[warn] " + Reset
errPrefix = Magenta + "%s\n" + Reset + Red + "[error] " + Reset errStr = Magenta + "%s\n" + Reset + Red + "[error] " + Reset
tracePrefix = Green + "%s\n" + Reset + YellowBold + "[%.3fms] " + Green + "[rows:%d]" + Reset + " %s" traceStr = Green + "%s\n" + Reset + Yellow + "[%.3fms] " + Blue + "[rows:%d]" + Reset + " %s"
traceErrPrefix = Magenta + "%s\n" + Reset + Redbold + "[%.3fms] " + Yellow + "[rows:%d]" + Reset + " %s" traceWarnStr = Green + "%s\n" + Reset + RedBold + "[%.3fms] " + Yellow + "[rows:%d]" + Magenta + " %s" + Reset
traceErrStr = RedBold + "%s " + MagentaBold + "%s\n" + Reset + Yellow + "[%.3fms] " + Blue + "[rows:%d]" + Reset + " %s"
} }
return logger{ return logger{
Writer: writer, Writer: writer,
Config: config, Config: config,
infoPrefix: infoPrefix, infoStr: infoStr,
warnPrefix: warnPrefix, warnStr: warnStr,
errPrefix: errPrefix, errStr: errStr,
tracePrefix: tracePrefix, traceStr: traceStr,
traceErrPrefix: traceErrPrefix, traceWarnStr: traceWarnStr,
traceErrStr: traceErrStr,
} }
} }
type logger struct { type logger struct {
Writer Writer
Config Config
infoPrefix, warnPrefix, errPrefix string infoStr, warnStr, errStr string
tracePrefix, traceErrPrefix string traceStr, traceErrStr, traceWarnStr string
} }
// LogMode log mode // LogMode log mode
@ -101,35 +105,38 @@ func (l logger) LogMode(level LogLevel) Interface {
// Info print info // Info print info
func (l logger) Info(msg string, data ...interface{}) { func (l logger) Info(msg string, data ...interface{}) {
if l.LogLevel >= Info { if l.LogLevel >= Info {
l.Printf(l.infoPrefix+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...) l.Printf(l.infoStr+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
} }
} }
// Warn print warn messages // Warn print warn messages
func (l logger) Warn(msg string, data ...interface{}) { func (l logger) Warn(msg string, data ...interface{}) {
if l.LogLevel >= Warn { if l.LogLevel >= Warn {
l.Printf(l.warnPrefix+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...) l.Printf(l.warnStr+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
} }
} }
// Error print error messages // Error print error messages
func (l logger) Error(msg string, data ...interface{}) { func (l logger) Error(msg string, data ...interface{}) {
if l.LogLevel >= Error { if l.LogLevel >= Error {
l.Printf(l.errPrefix+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...) l.Printf(l.errStr+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
} }
} }
// Trace print sql message // Trace print sql message
func (l logger) Trace(begin time.Time, fc func() (string, int64), err error) { func (l logger) Trace(begin time.Time, fc func() (string, int64), err error) {
if elapsed := time.Now().Sub(begin); elapsed > l.SlowThreshold && l.SlowThreshold != 0 { if l.LogLevel > 0 {
elapsed := time.Now().Sub(begin)
switch {
case err != nil:
sql, rows := fc() sql, rows := fc()
fileline := utils.FileWithLineNum() l.Printf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, rows, sql)
if err != nil { case elapsed > l.SlowThreshold && l.SlowThreshold != 0:
fileline += " " + err.Error()
}
l.Printf(l.traceErrPrefix, fileline, float64(elapsed.Nanoseconds())/1e6, rows, sql)
} else if l.LogLevel >= Info {
sql, rows := fc() sql, rows := fc()
l.Printf(l.tracePrefix, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql) l.Printf(l.traceWarnStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql)
case l.LogLevel >= Info:
sql, rows := fc()
l.Printf(l.traceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql)
}
} }
} }