gorm/logger/logger.go

136 lines
3.3 KiB
Go
Raw Normal View History

2020-01-28 18:01:35 +03:00
package logger
2020-01-31 01:35:25 +03:00
import (
"log"
"os"
2020-02-23 10:07:47 +03:00
"time"
"github.com/jinzhu/gorm/utils"
2020-01-31 01:35:25 +03:00
)
2020-02-23 10:07:47 +03:00
// 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"
Redbold = "\033[31;1m"
YellowBold = "\033[33;1m"
)
2020-01-30 10:14:48 +03:00
2020-02-23 10:07:47 +03:00
// LogLevel
type LogLevel int
2020-01-31 01:35:25 +03:00
2020-01-30 10:14:48 +03:00
const (
2020-02-23 07:39:26 +03:00
Error LogLevel = iota + 1
2020-01-30 10:14:48 +03:00
Warn
2020-02-23 07:39:26 +03:00
Info
2020-01-30 10:14:48 +03:00
)
2020-02-23 10:07:47 +03:00
// Writer log writer interface
type Writer interface {
Printf(string, ...interface{})
}
type Config struct {
SlowThreshold time.Duration
Colorful bool
LogLevel LogLevel
}
2020-01-28 18:01:35 +03:00
// Interface logger interface
type Interface interface {
2020-01-30 10:14:48 +03:00
LogMode(LogLevel) Interface
2020-01-31 01:35:25 +03:00
Info(string, ...interface{})
Warn(string, ...interface{})
Error(string, ...interface{})
2020-02-23 10:07:47 +03:00
Trace(begin time.Time, fc func() (string, int64), err error)
2020-01-31 01:35:25 +03:00
}
2020-02-23 10:07:47 +03:00
var Default = New(log.New(os.Stdout, "\r\n", log.LstdFlags), Config{
SlowThreshold: 100 * time.Millisecond,
2020-02-23 18:28:35 +03:00
LogLevel: Warn,
2020-02-23 10:07:47 +03:00
Colorful: true,
})
func New(writer Writer, config Config) Interface {
var (
infoPrefix = "%s\n[info] "
warnPrefix = "%s\n[warn] "
errPrefix = "%s\n[error] "
tracePrefix = "%s\n[%v] [rows:%d] %s"
traceErrPrefix = "%s\n[%v] [rows:%d] %s"
)
if config.Colorful {
2020-02-23 14:41:29 +03:00
infoPrefix = Green + "%s\n" + Reset + Green + "[info] " + Reset
warnPrefix = Blue + "%s\n" + Reset + Magenta + "[warn] " + Reset
errPrefix = Magenta + "%s\n" + Reset + Red + "[error] " + Reset
2020-02-23 10:07:47 +03:00
tracePrefix = Green + "%s\n" + Reset + YellowBold + "[%.3fms] " + Green + "[rows:%d]" + Reset + " %s"
traceErrPrefix = Magenta + "%s\n" + Reset + Redbold + "[%.3fms] " + Yellow + "[rows:%d]" + Reset + " %s"
}
return logger{
Writer: writer,
Config: config,
infoPrefix: infoPrefix,
warnPrefix: warnPrefix,
errPrefix: errPrefix,
tracePrefix: tracePrefix,
traceErrPrefix: traceErrPrefix,
}
2020-01-31 01:35:25 +03:00
}
2020-02-23 10:07:47 +03:00
type logger struct {
2020-01-31 01:35:25 +03:00
Writer
2020-02-23 10:07:47 +03:00
Config
infoPrefix, warnPrefix, errPrefix string
tracePrefix, traceErrPrefix string
2020-01-31 01:35:25 +03:00
}
2020-02-23 10:07:47 +03:00
// LogMode log mode
func (l logger) LogMode(level LogLevel) Interface {
2020-02-23 14:41:29 +03:00
l.LogLevel = level
return l
2020-01-31 01:35:25 +03:00
}
// Info print info
2020-02-23 10:07:47 +03:00
func (l logger) Info(msg string, data ...interface{}) {
if l.LogLevel >= Info {
2020-02-23 14:41:29 +03:00
l.Printf(l.infoPrefix+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
2020-01-31 01:35:25 +03:00
}
}
// Warn print warn messages
2020-02-23 10:07:47 +03:00
func (l logger) Warn(msg string, data ...interface{}) {
if l.LogLevel >= Warn {
2020-02-23 14:41:29 +03:00
l.Printf(l.warnPrefix+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
2020-01-31 01:35:25 +03:00
}
}
// Error print error messages
2020-02-23 10:07:47 +03:00
func (l logger) Error(msg string, data ...interface{}) {
if l.LogLevel >= Error {
2020-02-23 14:41:29 +03:00
l.Printf(l.errPrefix+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
2020-01-31 01:35:25 +03:00
}
2020-01-28 18:01:35 +03:00
}
2020-02-23 07:39:26 +03:00
2020-02-23 10:07:47 +03:00
// Trace print sql message
func (l logger) Trace(begin time.Time, fc func() (string, int64), err error) {
2020-03-04 17:16:39 +03:00
if elapsed := time.Now().Sub(begin); err != nil || (elapsed > l.SlowThreshold && l.SlowThreshold != 0) {
2020-02-23 10:07:47 +03:00
sql, rows := fc()
2020-02-23 14:41:29 +03:00
fileline := utils.FileWithLineNum()
if err != nil {
fileline += " " + err.Error()
}
l.Printf(l.traceErrPrefix, fileline, float64(elapsed.Nanoseconds())/1e6, rows, sql)
2020-02-23 10:07:47 +03:00
} else if l.LogLevel >= Info {
sql, rows := fc()
l.Printf(l.tracePrefix, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql)
2020-02-23 07:39:26 +03:00
}
}