From 7e2c74d894b48766a03682e64d99600d940db46b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=A5=87=E5=B3=B0?= <10515935zwj> Date: Sat, 24 Oct 2020 00:13:26 +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=BC=80=E5=8F=91=E8=80=85=E5=BF=AB?= =?UTF-8?q?=E9=80=9F=E9=9B=86=E6=88=90=E5=88=B0=E8=87=AA=E5=B7=B1=E7=9A=84?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E4=B9=8B=E5=90=8E=EF=BC=8C=E5=8F=AF=E9=9A=8F?= =?UTF-8?q?=E6=84=8F=E5=AF=B9=E6=97=A5=E5=BF=97=E6=8B=A6=E6=88=AA=E3=80=81?= =?UTF-8?q?=E4=BA=8C=E6=AC=A1=E5=A4=84=E7=90=86.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logger/logger.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/logger/logger.go b/logger/logger.go index 11619c92..ffbd2fa2 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -67,7 +67,7 @@ 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 = "%s\n[info] " 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.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 + }) +}