From bb35fc62bd65ac5cf9a439e4be345a58b7d3a889 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=A5=87=E5=B3=B0?= <10515935zwj> Date: Fri, 23 Oct 2020 23:03:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=20gorm=20=E8=87=AA=E5=B8=A6=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=BF=9B=E8=A1=8C=E4=BA=86=E5=8A=A0=E5=BC=BA=EF=BC=8C?= =?UTF-8?q?=E5=85=81=E8=AE=B8=E9=80=9A=E8=BF=87New=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=BB=98=E8=AE=A4=E7=9A=84=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F,=E5=85=81=E8=AE=B8=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E8=80=85=E5=BF=AB=E9=80=9F=E9=9B=86=E6=88=90=E5=88=B0=E8=87=AA?= =?UTF-8?q?=E5=B7=B1=E7=9A=84=E9=A1=B9=E7=9B=AE=E4=B9=8B=E5=90=8E=EF=BC=8C?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=97=A5=E5=BF=97=E7=9A=84=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E3=80=81=E9=87=8D=E5=86=99=E6=9B=B4=E5=8A=A0=E7=AE=80=E5=8D=95?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logger/logger.go | 63 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index 6ebccc46..c05936d4 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -67,14 +67,14 @@ var ( Recorder = traceRecorder{Interface: Default, BeginAt: time.Now()} ) -func New(writer Writer, config Config) Interface { +func New(writer Writer, config Config, v ...Options) Interface { var ( - infoStr = "[info] %s\n " - warnStr = "[warn] %s\n" - errStr = "[error] %s\n" - traceStr = "[traceStr] %s [%.3fms] [rows:%v] %s\n" - traceWarnStr = "[traceWarn] %s %s [%.3fms] [rows:%v] %s\n" - traceErrStr = "[traceErr] %s %s [%.3fms] [rows:%v] %s\n" + 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" ) 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.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 + }) +}