|
|
@ -4,7 +4,6 @@ import ( |
|
|
|
"context" |
|
|
|
"fmt" |
|
|
|
"gin-vue-admin/global" |
|
|
|
"go.uber.org/zap" |
|
|
|
"gorm.io/gorm/logger" |
|
|
|
"gorm.io/gorm/utils" |
|
|
|
"io/ioutil" |
|
|
@ -13,11 +12,6 @@ import ( |
|
|
|
"time" |
|
|
|
) |
|
|
|
|
|
|
|
// writer log writer interface
|
|
|
|
type writer interface { |
|
|
|
Printf(string, ...interface{}) |
|
|
|
} |
|
|
|
|
|
|
|
type config struct { |
|
|
|
SlowThreshold time.Duration |
|
|
|
Colorful bool |
|
|
@ -34,27 +28,27 @@ var ( |
|
|
|
Recorder = traceRecorder{Interface: Default, BeginAt: time.Now()} |
|
|
|
) |
|
|
|
|
|
|
|
func New(writer writer, config config) logger.Interface { |
|
|
|
func New(writer logger.Writer, config config) logger.Interface { |
|
|
|
var ( |
|
|
|
infoStr = "%s\n[info] " |
|
|
|
warnStr = "%s\n[warn] " |
|
|
|
errStr = "%s\n[error] " |
|
|
|
traceStr = "%s\n[%.3fms] [rows:%v] %s" |
|
|
|
traceWarnStr = "%s %s\n[%.3fms] [rows:%v] %s" |
|
|
|
traceErrStr = "%s %s\n[%.3fms] [rows:%v] %s" |
|
|
|
traceStr = "%s\n[%.3fms] [rows:%v] %s\n" |
|
|
|
traceWarnStr = "%s %s\n[%.3fms] [rows:%v] %s\n" |
|
|
|
traceErrStr = "%s %s\n[%.3fms] [rows:%v] %s\n" |
|
|
|
) |
|
|
|
|
|
|
|
if config.Colorful { |
|
|
|
infoStr = logger.Green + "%s\n" + logger.Reset + logger.Green + "[info] " + logger.Reset |
|
|
|
warnStr = logger.BlueBold + "%s\n" + logger.Reset + logger.Magenta + "[warn] " + logger.Reset |
|
|
|
errStr = logger.Magenta + "%s\n" + logger.Reset + logger.Red + "[error] " + logger.Reset |
|
|
|
traceStr = logger.Green + "%s\n" + logger.Reset + logger.Yellow + "[%.3fms] " + logger.BlueBold + "[rows:%v]" + logger.Reset + " %s" |
|
|
|
traceWarnStr = logger.Green + "%s " + logger.Yellow + "%s\n" + logger.Reset + logger.RedBold + "[%.3fms] " + logger.Yellow + "[rows:%v]" + logger.Magenta + " %s" + logger.Reset |
|
|
|
traceErrStr = logger.RedBold + "%s " + logger.MagentaBold + "%s\n" + logger.Reset + logger.Yellow + "[%.3fms] " + logger.BlueBold + "[rows:%v]" + logger.Reset + " %s" |
|
|
|
traceStr = logger.Green + "%s\n" + logger.Reset + logger.Yellow + "[%.3fms] " + logger.BlueBold + "[rows:%v]" + logger.Reset + " %s\n" |
|
|
|
traceWarnStr = logger.Green + "%s " + logger.Yellow + "%s\n" + logger.Reset + logger.RedBold + "[%.3fms] " + logger.Yellow + "[rows:%v]" + logger.Magenta + " %s\n" + logger.Reset |
|
|
|
traceErrStr = logger.RedBold + "%s " + logger.MagentaBold + "%s\n" + logger.Reset + logger.Yellow + "[%.3fms] " + logger.BlueBold + "[rows:%v]" + logger.Reset + " %s\n" |
|
|
|
} |
|
|
|
|
|
|
|
return &customLogger{ |
|
|
|
writer: writer, |
|
|
|
return &_logger{ |
|
|
|
Writer: writer, |
|
|
|
config: config, |
|
|
|
infoStr: infoStr, |
|
|
|
warnStr: warnStr, |
|
|
@ -65,43 +59,43 @@ func New(writer writer, config config) logger.Interface { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
type customLogger struct { |
|
|
|
writer |
|
|
|
type _logger struct { |
|
|
|
config |
|
|
|
logger.Writer |
|
|
|
infoStr, warnStr, errStr string |
|
|
|
traceStr, traceErrStr, traceWarnStr string |
|
|
|
} |
|
|
|
|
|
|
|
// LogMode log mode
|
|
|
|
func (c *customLogger) LogMode(level logger.LogLevel) logger.Interface { |
|
|
|
func (c *_logger) LogMode(level logger.LogLevel) logger.Interface { |
|
|
|
newLogger := *c |
|
|
|
newLogger.LogLevel = level |
|
|
|
return &newLogger |
|
|
|
} |
|
|
|
|
|
|
|
// Info print info
|
|
|
|
func (c *customLogger) Info(ctx context.Context, message string, data ...interface{}) { |
|
|
|
func (c *_logger) Info(ctx context.Context, message string, data ...interface{}) { |
|
|
|
if c.LogLevel >= logger.Info { |
|
|
|
c.Printf(c.infoStr+message, append([]interface{}{utils.FileWithLineNum()}, data...)...) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Warn print warn messages
|
|
|
|
func (c *customLogger) Warn(ctx context.Context, message string, data ...interface{}) { |
|
|
|
func (c *_logger) Warn(ctx context.Context, message string, data ...interface{}) { |
|
|
|
if c.LogLevel >= logger.Warn { |
|
|
|
c.Printf(c.warnStr+message, append([]interface{}{utils.FileWithLineNum()}, data...)...) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Error print error messages
|
|
|
|
func (c *customLogger) Error(ctx context.Context, message string, data ...interface{}) { |
|
|
|
func (c *_logger) Error(ctx context.Context, message string, data ...interface{}) { |
|
|
|
if c.LogLevel >= logger.Error { |
|
|
|
c.Printf(c.errStr+message, append([]interface{}{utils.FileWithLineNum()}, data...)...) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Trace print sql message
|
|
|
|
func (c *customLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { |
|
|
|
func (c *_logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { |
|
|
|
if c.LogLevel > 0 { |
|
|
|
elapsed := time.Since(begin) |
|
|
|
switch { |
|
|
@ -131,35 +125,11 @@ func (c *customLogger) Trace(ctx context.Context, begin time.Time, fc func() (st |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (c *customLogger) Printf(message string, data ...interface{}) { |
|
|
|
if global.GVA_CONFIG.Mysql.LogZap != "" { |
|
|
|
switch len(data) { |
|
|
|
case 0: |
|
|
|
global.GVA_LOG.Info(message) |
|
|
|
case 1: |
|
|
|
global.GVA_LOG.Info("gorm", zap.Any("src", data[0])) |
|
|
|
case 2: |
|
|
|
global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1])) |
|
|
|
case 3: |
|
|
|
global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1]), zap.Any("rows", data[2])) |
|
|
|
case 4: |
|
|
|
global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1]), zap.Any("rows", data[2]), zap.Any("sql", data[3])) |
|
|
|
} |
|
|
|
return |
|
|
|
} |
|
|
|
switch len(data) { |
|
|
|
case 0: |
|
|
|
c.writer.Printf(message, "") |
|
|
|
case 1: |
|
|
|
c.writer.Printf(message, data[0]) |
|
|
|
case 2: |
|
|
|
c.writer.Printf(message, data[0], data[1]) |
|
|
|
case 3: |
|
|
|
c.writer.Printf(message, data[0], data[1], data[2]) |
|
|
|
case 4: |
|
|
|
c.writer.Printf(message, data[0], data[1], data[2], data[3]) |
|
|
|
case 5: |
|
|
|
c.writer.Printf(message, data[0], data[1], data[2], data[3], data[4]) |
|
|
|
func (c *_logger) Printf(message string, data ...interface{}) { |
|
|
|
if global.GVA_CONFIG.Mysql.LogZap { |
|
|
|
global.GVA_LOG.Info(fmt.Sprintf(message, data...)) |
|
|
|
} else { |
|
|
|
c.Writer.Printf(message, data...) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|