|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package loggers
-
- import (
- "context"
- "github.com/tal-tech/go-zero/core/logx"
- ll "gorm.io/gorm/logger"
- "gorm.io/gorm/utils"
- "time"
- )
-
- // LogLevel
- type LogLevel int
-
- // Writer log writer interface
- type Writer interface {
- Printf(string, ...interface{})
- }
-
- // 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 (
- //Discard = New(log.New(ioutil.Discard, "", log.LstdFlags), Config{})
- //Default = New(log.New(os.Stdout, "\r\n", log.LstdFlags), Config{
- // SlowThreshold: 100 * time.Millisecond,
- // LogLevel: Warn,
- // Colorful: true,
- //})
- //Recorder = traceRecorder{Interface: Default}
- )
-
- func New() ll.Interface {
- return &logger{}
- }
-
- type logger struct {
- Writer
- ll.Config
- infoStr, warnStr, errStr string
- traceStr, traceErrStr, traceWarnStr string
- }
-
- // LogMode log mode
- func (l logger) LogMode(level ll.LogLevel) ll.Interface {
- newlogger := l
- newlogger.LogLevel = level
- return &newlogger
- }
-
- // Info print info
- func (l logger) Info(ctx context.Context, msg string, data ...interface{}) {
- logx.Infof(l.infoStr+msg, data...)
- }
-
- // Warn print warn messages
- func (l logger) Warn(ctx context.Context, msg string, data ...interface{}) {
- logx.Infof(l.warnStr+msg, data...)
- }
-
- // Error print error messages
- func (l logger) Error(ctx context.Context, msg string, data ...interface{}) {
- logx.Errorf(l.errStr+msg, 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.Since(begin)
- switch {
- case err != nil && l.LogLevel >= ll.Error:
- sql, rows := fc()
- if rows == -1 {
- logx.Error(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", sql)
- //l.Printf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", sql)
- } else {
- logx.Error(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, rows, sql)
- //l.Printf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, rows, sql)
- }
- case elapsed > l.SlowThreshold && l.SlowThreshold != 0 && l.LogLevel >= ll.Warn:
- sql, rows := fc()
- if rows == -1 {
- logx.Info(l.traceWarnStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, "-", sql)
- } else {
- logx.Info(l.traceWarnStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql)
- }
- case l.LogLevel >= ll.Info:
- sql, rows := fc()
- if rows == -1 {
- logx.Info(l.traceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, "-", sql)
- } else {
- logx.Info(l.traceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql)
- }
- }
- }
- }
-
- type traceRecorder struct {
- Interface
- BeginAt time.Time
- SQL string
- RowsAffected int64
- Err error
- }
-
- func (l traceRecorder) New() *traceRecorder {
- return &traceRecorder{Interface: l.Interface}
- }
-
- func (l *traceRecorder) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
- l.BeginAt = begin
- l.SQL, l.RowsAffected = fc()
- l.Err = err
- }
|