对 gorm 自带日志进行了加强,允许通过New函数修改默认的日志格式,开发者快速集成到自己的项目之后,可随意对日志拦截、二次处理.
This commit is contained in:
parent
cb591a7129
commit
7e2c74d894
@ -67,7 +67,7 @@ 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 = "%s\n[info] "
|
infoStr = "%s\n[info] "
|
||||||
warnStr = "%s\n[warn] "
|
warnStr = "%s\n[warn] "
|
||||||
@ -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 log fomart
|
||||||
|
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