From b75aa1d1416936cec35840df6da4900ffe38af2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=8E=E7=99=BD=E5=8D=97?= <1762386214@qq.com> Date: Tue, 5 Oct 2021 21:36:06 +0800 Subject: [PATCH] feat(logx) make struct without new --- pkg/logx/define.go | 32 +++++------------- pkg/logx/logx.go | 84 +++++++++++++++++++++++++++++----------------- 2 files changed, 62 insertions(+), 54 deletions(-) diff --git a/pkg/logx/define.go b/pkg/logx/define.go index e450566..c7d38b4 100644 --- a/pkg/logx/define.go +++ b/pkg/logx/define.go @@ -2,38 +2,24 @@ package logx import ( "gitee.com/timedb/wheatCache/pkg/event" - "time" + middle_msg "gitee.com/timedb/wheatCache/pkg/middle-msg" ) type LogLevelState int8 -const ( - DebugState = LogLevelState(iota) -) - -type logger struct { - Stack string - Msg string - Level LogLevelState - LogTime time.Time -} - type upLogger struct { - logger produce *event.ProduceInterface } -type logFunc interface { - With(p *event.ProduceInterface) *upLogger +type logInterface interface { + Debug(msg interface{}) *middle_msg.LogContext + Info(msg interface{}) *middle_msg.LogContext + Warn(msg interface{}) *middle_msg.LogContext + Error(msg interface{}) *middle_msg.LogContext - Debug(msg interface{}) - Info(msg interface{}) - Warn(msg interface{}) - Error(msg interface{}) - - Print(level string, msg interface{}) + Print(level string, msg interface{}) *middle_msg.LogContext } -func NewLogger() *logger { - return &logger{} +func NewLogger() *upLogger { + return &upLogger{} } diff --git a/pkg/logx/logx.go b/pkg/logx/logx.go index 6b5f4ed..3097df1 100644 --- a/pkg/logx/logx.go +++ b/pkg/logx/logx.go @@ -3,23 +3,67 @@ package logx import ( "fmt" "gitee.com/timedb/wheatCache/pkg/event" + "gitee.com/timedb/wheatCache/pkg/middle-msg" "runtime" "time" ) -type logContext struct { - level string - dataStr string - msg string - route string +func With(p *event.ProduceInterface) *upLogger { + return &upLogger{ + p, + } } -func (l *logger) With(p *event.ProduceInterface) *upLogger { - return &upLogger{} +func (l *upLogger) Debug(msg interface{}) *middle_msg.LogContext { + return l.Print("DEBUG", msg) +} +func (l *upLogger) Info(msg interface{}) *middle_msg.LogContext { + return l.Print("INFO", msg) +} +func (l *upLogger) Warn(msg interface{}) *middle_msg.LogContext { + return l.Print("WARN", msg) +} +func (l *upLogger) Error(msg interface{}) *middle_msg.LogContext { + return l.Print("ERROR", msg) } -func (l *logger) Print(level string, msg interface{}) { +func (l *upLogger) Print(level string, msg interface{}) *middle_msg.LogContext { + place := findPlace() + datetime := fmt.Sprintf("%s", time.Now())[0:19] + fmt.Println(level, datetime, msg, place) + + return &middle_msg.LogContext{ + Level: level, + Data: time.Now(), + Msg: fmt.Sprintf("%v", msg), + Route: place, + } +} + +func Debug(msg interface{}) { + Print("DEBUG", msg) +} +func Info(msg interface{}) { + Print("INFO", msg) +} +func Warn(msg interface{}) { + Print("WARN", msg) +} +func Error(msg interface{}) { + Print("ERROR", msg) +} + +func Print(level string, msg interface{}) { + + place := findPlace() + + datetime := fmt.Sprintf("%s", time.Now())[0:19] + + fmt.Println(level, datetime, msg, place) +} + +func findPlace() string { var ( place string i = 0 @@ -34,27 +78,5 @@ func (l *logger) Print(level string, msg interface{}) { place = fmt.Sprintf("%s:%d\n%s", file, line, place) } - datetime := fmt.Sprintf("%s", time.Now())[0:19] - - fmt.Println(level, datetime, msg, place) - - //_ = &logContext{ - // level: level, - // dataStr: datetime, - // msg: fmt.Sprintf("%v", msg), - // route: place, - //} -} - -func (l *logger) Debug(msg interface{}) { - l.Print("DEBUG", msg) -} -func (l *logger) Info(msg interface{}) { - l.Print("INFO", msg) -} -func (l *logger) Warn(msg interface{}) { - l.Print("WARN", msg) -} -func (l *logger) Error(msg interface{}) { - l.Print("ERROR", msg) + return place }