refactor: 暴露getWriter方法出去

main v1.1.7
NoahLan 1 year ago
parent 2a31984fc7
commit 1bd773e3ac

@ -53,7 +53,7 @@ type (
// Alert alerts v in alert level, and the message is written to error log. // Alert alerts v in alert level, and the message is written to error log.
func Alert(v string) { func Alert(v string) {
getWriter().Alert(v) GetWriter().Alert(v)
} }
// Close closes the logging. // Close closes the logging.
@ -223,7 +223,7 @@ func Must(err error) {
msg := fmt.Sprintf("%+v\n\n%s", err.Error(), debug.Stack()) msg := fmt.Sprintf("%+v\n\n%s", err.Error(), debug.Stack())
log.Print(msg) log.Print(msg)
getWriter().Severe(msg) GetWriter().Severe(msg)
if ExitOnFatal.True() { if ExitOnFatal.True() {
os.Exit(1) os.Exit(1)
@ -413,7 +413,7 @@ func createOutput(path string) (io.WriteCloser, error) {
return NewLogger(path, rule, options.gzipEnabled) return NewLogger(path, rule, options.gzipEnabled)
} }
func getWriter() Writer { func GetWriter() Writer {
w := writer.Load() w := writer.Load()
if w == nil { if w == nil {
w = writer.StoreIfNil(newConsoleWriter()) 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. // 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. // The caller should check shallLog before calling this function.
func writeDebug(val any, fields ...LogField) { func writeDebug(val any, fields ...LogField) {
getWriter().Debug(val, addCaller(fields...)...) GetWriter().Debug(val, addCaller(fields...)...)
} }
// writeError writes v into error log. // 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. // 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. // The caller should check shallLog before calling this function.
func writeError(val any, fields ...LogField) { func writeError(val any, fields ...LogField) {
getWriter().Error(val, addCaller(fields...)...) GetWriter().Error(val, addCaller(fields...)...)
} }
// writeInfo writes v into info log. // 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. // 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. // The caller should check shallLog before calling this function.
func writeInfo(val any, fields ...LogField) { func writeInfo(val any, fields ...LogField) {
getWriter().Info(val, addCaller(fields...)...) GetWriter().Info(val, addCaller(fields...)...)
} }
// writeSevere writes v into severe log. // 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. // 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. // The caller should check shallLog before calling this function.
func writeSevere(msg string) { 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. // 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. // 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. // The caller should check shallLog before calling this function.
func writeSlow(val any, fields ...LogField) { func writeSlow(val any, fields ...LogField) {
getWriter().Slow(val, addCaller(fields...)...) GetWriter().Slow(val, addCaller(fields...)...)
} }
// writeStack writes v into stack log. // 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. // 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. // The caller should check shallLog before calling this function.
func writeStack(msg string) { 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. // 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. // 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. // The caller should check shallLog before calling this function.
func writeStat(msg string) { func writeStat(msg string) {
getWriter().Stat(msg, addCaller()...) GetWriter().Stat(msg, addCaller()...)
} }

@ -156,24 +156,24 @@ func (l *richLogger) buildFields(fields ...LogField) []LogField {
func (l *richLogger) debug(v any, fields ...LogField) { func (l *richLogger) debug(v any, fields ...LogField) {
if shallLog(DebugLevel) { if shallLog(DebugLevel) {
getWriter().Debug(v, l.buildFields(fields...)...) GetWriter().Debug(v, l.buildFields(fields...)...)
} }
} }
func (l *richLogger) err(v any, fields ...LogField) { func (l *richLogger) err(v any, fields ...LogField) {
if shallLog(ErrorLevel) { if shallLog(ErrorLevel) {
getWriter().Error(v, l.buildFields(fields...)...) GetWriter().Error(v, l.buildFields(fields...)...)
} }
} }
func (l *richLogger) info(v any, fields ...LogField) { func (l *richLogger) info(v any, fields ...LogField) {
if shallLog(InfoLevel) { if shallLog(InfoLevel) {
getWriter().Info(v, l.buildFields(fields...)...) GetWriter().Info(v, l.buildFields(fields...)...)
} }
} }
func (l *richLogger) slow(v any, fields ...LogField) { func (l *richLogger) slow(v any, fields ...LogField) {
if shallLog(ErrorLevel) { if shallLog(ErrorLevel) {
getWriter().Slow(v, l.buildFields(fields...)...) GetWriter().Slow(v, l.buildFields(fields...)...)
} }
} }

Loading…
Cancel
Save