mirror of https://github.com/go-gorm/gorm.git
144 lines
3.7 KiB
Go
144 lines
3.7 KiB
Go
package logger
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/jinzhu/gorm/utils"
|
|
)
|
|
|
|
// Colors
|
|
const (
|
|
Reset = "\033[0m"
|
|
Red = "\033[31m"
|
|
Green = "\033[32m"
|
|
Yellow = "\033[33m"
|
|
Blue = "\033[34m"
|
|
Magenta = "\033[35m"
|
|
Cyan = "\033[36m"
|
|
White = "\033[37m"
|
|
MagentaBold = "\033[35;1m"
|
|
RedBold = "\033[31;1m"
|
|
YellowBold = "\033[33;1m"
|
|
)
|
|
|
|
// LogLevel
|
|
type LogLevel int
|
|
|
|
const (
|
|
Error LogLevel = iota + 1
|
|
Warn
|
|
Info
|
|
)
|
|
|
|
// Writer log writer interface
|
|
type Writer interface {
|
|
Printf(string, ...interface{})
|
|
}
|
|
|
|
type Config struct {
|
|
SlowThreshold time.Duration
|
|
Colorful bool
|
|
LogLevel LogLevel
|
|
}
|
|
|
|
// Interface logger interface
|
|
type Interface interface {
|
|
LogMode(LogLevel) Interface
|
|
Info(context.Context, string, ...interface{})
|
|
Warn(context.Context, string, ...interface{})
|
|
Error(context.Context, string, ...interface{})
|
|
Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)
|
|
}
|
|
|
|
var Default = New(log.New(os.Stdout, "\r\n", log.LstdFlags), Config{
|
|
SlowThreshold: 100 * time.Millisecond,
|
|
LogLevel: Warn,
|
|
Colorful: true,
|
|
})
|
|
|
|
func New(writer Writer, config Config) Interface {
|
|
var (
|
|
infoStr = "%s\n[info] "
|
|
warnStr = "%s\n[warn] "
|
|
errStr = "%s\n[error] "
|
|
traceStr = "%s\n[%v] [rows:%d] %s"
|
|
traceWarnStr = "%s\n[%v] [rows:%d] %s"
|
|
traceErrStr = "%s %s\n[%v] [rows:%d] %s"
|
|
)
|
|
|
|
if config.Colorful {
|
|
infoStr = Green + "%s\n" + Reset + Green + "[info] " + Reset
|
|
warnStr = Blue + "%s\n" + Reset + Magenta + "[warn] " + Reset
|
|
errStr = Magenta + "%s\n" + Reset + Red + "[error] " + Reset
|
|
traceStr = Green + "%s\n" + Reset + Yellow + "[%.3fms] " + Blue + "[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{
|
|
Writer: writer,
|
|
Config: config,
|
|
infoStr: infoStr,
|
|
warnStr: warnStr,
|
|
errStr: errStr,
|
|
traceStr: traceStr,
|
|
traceWarnStr: traceWarnStr,
|
|
traceErrStr: traceErrStr,
|
|
}
|
|
}
|
|
|
|
type logger struct {
|
|
Writer
|
|
Config
|
|
infoStr, warnStr, errStr string
|
|
traceStr, traceErrStr, traceWarnStr string
|
|
}
|
|
|
|
// LogMode log mode
|
|
func (l logger) LogMode(level LogLevel) Interface {
|
|
l.LogLevel = level
|
|
return l
|
|
}
|
|
|
|
// Info print info
|
|
func (l logger) Info(ctx context.Context, msg string, data ...interface{}) {
|
|
if l.LogLevel >= Info {
|
|
l.Printf(l.infoStr+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
|
|
}
|
|
}
|
|
|
|
// Warn print warn messages
|
|
func (l logger) Warn(ctx context.Context, msg string, data ...interface{}) {
|
|
if l.LogLevel >= Warn {
|
|
l.Printf(l.warnStr+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
|
|
}
|
|
}
|
|
|
|
// Error print error messages
|
|
func (l logger) Error(ctx context.Context, msg string, data ...interface{}) {
|
|
if l.LogLevel >= Error {
|
|
l.Printf(l.errStr+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
|
|
}
|
|
}
|
|
|
|
// Trace print sql message
|
|
func (l logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
|
|
if l.LogLevel > 0 {
|
|
elapsed := time.Now().Sub(begin)
|
|
switch {
|
|
case err != nil:
|
|
sql, rows := fc()
|
|
l.Printf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, rows, sql)
|
|
case elapsed > l.SlowThreshold && l.SlowThreshold != 0 && l.LogLevel >= Warn:
|
|
sql, rows := fc()
|
|
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)
|
|
}
|
|
}
|
|
}
|