对 gorm 自带日志进行了加强,允许通过New函数修改默认的日志格式,允许开发者快速集成到自己的项目之后,相关日志的拦截、重写更加简单.
This commit is contained in:
parent
f982133c16
commit
bb35fc62bd
@ -67,14 +67,14 @@ var (
|
|||||||
Recorder = traceRecorder{Interface: Default, BeginAt: time.Now()}
|
Recorder = traceRecorder{Interface: Default, BeginAt: time.Now()}
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(writer Writer, config Config) Interface {
|
func New(writer Writer, config Config, v ...Options) Interface {
|
||||||
var (
|
var (
|
||||||
infoStr = "[info] %s\n "
|
infoStr = "%s\n[info] "
|
||||||
warnStr = "[warn] %s\n"
|
warnStr = "%s\n[warn] "
|
||||||
errStr = "[error] %s\n"
|
errStr = "%s\n[error] "
|
||||||
traceStr = "[traceStr] %s [%.3fms] [rows:%v] %s\n"
|
traceStr = "%s\n[%.3fms] [rows:%v] %s"
|
||||||
traceWarnStr = "[traceWarn] %s %s [%.3fms] [rows:%v] %s\n"
|
traceWarnStr = "%s %s\n[%.3fms] [rows:%v] %s"
|
||||||
traceErrStr = "[traceErr] %s %s [%.3fms] [rows:%v] %s\n"
|
traceErrStr = "%s %s\n[%.3fms] [rows:%v] %s"
|
||||||
)
|
)
|
||||||
|
|
||||||
if config.Colorful {
|
if config.Colorful {
|
||||||
@ -181,3 +181,52 @@ func (l *traceRecorder) Trace(ctx context.Context, begin time.Time, fc func() (s
|
|||||||
l.SQL, l.RowsAffected = fc()
|
l.SQL, l.RowsAffected = fc()
|
||||||
l.Err = err
|
l.Err = err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The developer can modify the log format through the following functions
|
||||||
|
// so easy to integrate gorm into their project
|
||||||
|
|
||||||
|
type Options interface {
|
||||||
|
apply(*logger)
|
||||||
|
}
|
||||||
|
type OptionFunc func(log *logger)
|
||||||
|
|
||||||
|
func (f OptionFunc) apply(log *logger) {
|
||||||
|
f(log)
|
||||||
|
}
|
||||||
|
|
||||||
|
// remodify
|
||||||
|
func SetInfoStrFormat(format string) Options {
|
||||||
|
return OptionFunc(func(log *logger) {
|
||||||
|
log.infoStr = format
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetWarnStrFormat(format string) Options {
|
||||||
|
return OptionFunc(func(log *logger) {
|
||||||
|
log.warnStr = format
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetErrStrFormat(format string) Options {
|
||||||
|
return OptionFunc(func(log *logger) {
|
||||||
|
log.errStr = format
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetTraceStrFormat(format string) Options {
|
||||||
|
return OptionFunc(func(log *logger) {
|
||||||
|
log.traceStr = format
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetTracWarnStrFormat(format string) Options {
|
||||||
|
return OptionFunc(func(log *logger) {
|
||||||
|
log.traceWarnStr = format
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetTracErrStrFormat(format string) Options {
|
||||||
|
return OptionFunc(func(log *logger) {
|
||||||
|
log.traceErrStr = format
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user