From 1bd773e3acd7592a83e583d1f1219d1502a51c92 Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Thu, 20 Jul 2023 18:03:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=9A=B4=E9=9C=B2getWriter?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=87=BA=E5=8E=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nlog/logs.go | 20 ++++++++++---------- nlog/richlogger.go | 8 ++++---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/nlog/logs.go b/nlog/logs.go index 15d86d2..3f2d4db 100644 --- a/nlog/logs.go +++ b/nlog/logs.go @@ -53,7 +53,7 @@ type ( // Alert alerts v in alert level, and the message is written to error log. func Alert(v string) { - getWriter().Alert(v) + GetWriter().Alert(v) } // Close closes the logging. @@ -223,7 +223,7 @@ func Must(err error) { msg := fmt.Sprintf("%+v\n\n%s", err.Error(), debug.Stack()) log.Print(msg) - getWriter().Severe(msg) + GetWriter().Severe(msg) if ExitOnFatal.True() { os.Exit(1) @@ -413,7 +413,7 @@ func createOutput(path string) (io.WriteCloser, error) { return NewLogger(path, rule, options.gzipEnabled) } -func getWriter() Writer { +func GetWriter() Writer { w := writer.Load() if w == nil { w = writer.StoreIfNil(newConsoleWriter()) @@ -477,7 +477,7 @@ func shallLogStat() bool { // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled. // The caller should check shallLog before calling this function. func writeDebug(val any, fields ...LogField) { - getWriter().Debug(val, addCaller(fields...)...) + GetWriter().Debug(val, addCaller(fields...)...) } // writeError writes v into error log. @@ -485,7 +485,7 @@ func writeDebug(val any, fields ...LogField) { // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled. // The caller should check shallLog before calling this function. func writeError(val any, fields ...LogField) { - getWriter().Error(val, addCaller(fields...)...) + GetWriter().Error(val, addCaller(fields...)...) } // writeInfo writes v into info log. @@ -493,7 +493,7 @@ func writeError(val any, fields ...LogField) { // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled. // The caller should check shallLog before calling this function. func writeInfo(val any, fields ...LogField) { - getWriter().Info(val, addCaller(fields...)...) + GetWriter().Info(val, addCaller(fields...)...) } // writeSevere writes v into severe log. @@ -501,7 +501,7 @@ func writeInfo(val any, fields ...LogField) { // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled. // The caller should check shallLog before calling this function. func writeSevere(msg string) { - getWriter().Severe(fmt.Sprintf("%s\n%s", msg, string(debug.Stack()))) + GetWriter().Severe(fmt.Sprintf("%s\n%s", msg, string(debug.Stack()))) } // writeSlow writes v into slow log. @@ -509,7 +509,7 @@ func writeSevere(msg string) { // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled. // The caller should check shallLog before calling this function. func writeSlow(val any, fields ...LogField) { - getWriter().Slow(val, addCaller(fields...)...) + GetWriter().Slow(val, addCaller(fields...)...) } // writeStack writes v into stack log. @@ -517,7 +517,7 @@ func writeSlow(val any, fields ...LogField) { // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled. // The caller should check shallLog before calling this function. func writeStack(msg string) { - getWriter().Stack(fmt.Sprintf("%s\n%s", msg, string(debug.Stack()))) + GetWriter().Stack(fmt.Sprintf("%s\n%s", msg, string(debug.Stack()))) } // writeStat writes v into stat log. @@ -525,5 +525,5 @@ func writeStack(msg string) { // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled. // The caller should check shallLog before calling this function. func writeStat(msg string) { - getWriter().Stat(msg, addCaller()...) + GetWriter().Stat(msg, addCaller()...) } diff --git a/nlog/richlogger.go b/nlog/richlogger.go index d2205db..bd3c33b 100644 --- a/nlog/richlogger.go +++ b/nlog/richlogger.go @@ -156,24 +156,24 @@ func (l *richLogger) buildFields(fields ...LogField) []LogField { func (l *richLogger) debug(v any, fields ...LogField) { if shallLog(DebugLevel) { - getWriter().Debug(v, l.buildFields(fields...)...) + GetWriter().Debug(v, l.buildFields(fields...)...) } } func (l *richLogger) err(v any, fields ...LogField) { if shallLog(ErrorLevel) { - getWriter().Error(v, l.buildFields(fields...)...) + GetWriter().Error(v, l.buildFields(fields...)...) } } func (l *richLogger) info(v any, fields ...LogField) { if shallLog(InfoLevel) { - getWriter().Info(v, l.buildFields(fields...)...) + GetWriter().Info(v, l.buildFields(fields...)...) } } func (l *richLogger) slow(v any, fields ...LogField) { if shallLog(ErrorLevel) { - getWriter().Slow(v, l.buildFields(fields...)...) + GetWriter().Slow(v, l.buildFields(fields...)...) } }