diff --git a/pkg/logx/define.go b/pkg/logx/define.go index 7ba2d23..0ced49c 100644 --- a/pkg/logx/define.go +++ b/pkg/logx/define.go @@ -2,20 +2,37 @@ package logx import ( "context" + "fmt" "gitee.com/timedb/wheatCache/pkg/event" + "github.com/spf13/viper" + "sync" ) type LogLevelState int8 +var ( + once sync.Once + stath []string +) + type upLogger struct { + ctx context.Context produce event.ProduceInterface } -type logInterface interface { - Debug(ctx context.Context, msg interface{}) - Info(ctx context.Context, msg interface{}) - Warn(ctx context.Context, msg interface{}) - Error(ctx context.Context, msg interface{}) - - Print(level string, ctx context.Context, msg interface{}) +func init() { + once.Do(func() { + stath = viper.GetStringSlice("logPrint.stath") + fmt.Println(stath) + }) +} + +type logInterface interface { + Debug(format string, msg ...interface{}) + Info(format string, msg ...interface{}) + Warn(format string, msg ...interface{}) + Error(format string, msg ...interface{}) + Panic(format string, msg ...interface{}) + + Print(level string, format string, msg ...interface{}) } diff --git a/pkg/logx/logx.go b/pkg/logx/logx.go index 7e6b8db..a02626b 100644 --- a/pkg/logx/logx.go +++ b/pkg/logx/logx.go @@ -6,62 +6,75 @@ import ( "gitee.com/timedb/wheatCache/pkg/event" middleMsg "gitee.com/timedb/wheatCache/pkg/middle-msg" "runtime" + "strings" "time" ) -func With(p event.ProduceInterface) *upLogger { +func With(ctx context.Context, p event.ProduceInterface) *upLogger { return &upLogger{ + ctx: ctx, produce: p, } } -func (l *upLogger) Debug(ctx context.Context, msg interface{}) { - l.Print("DEBUG", ctx, msg) +func (l *upLogger) Debug(format string, msg ...interface{}) { + l.Print("DEBUG", format, msg...) } -func (l *upLogger) Info(ctx context.Context, msg interface{}) { - l.Print("INFO", ctx, msg) +func (l *upLogger) Info(format string, msg ...interface{}) { + l.Print("INFO", format, msg...) } -func (l *upLogger) Warn(ctx context.Context, msg interface{}) { - l.Print("WARN", ctx, msg) +func (l *upLogger) Warn(format string, msg ...interface{}) { + l.Print("WARN", format, msg...) } -func (l *upLogger) Error(ctx context.Context, msg interface{}) { - l.Print("ERROR", ctx, msg) +func (l *upLogger) Error(format string, msg ...interface{}) { + l.Print("ERROR", format, msg...) +} +func (l *upLogger) Panic(format string, msg ...interface{}) { + Print("ERROR", format, msg...) } -func (l *upLogger) Print(level string, ctx context.Context, msg interface{}) { +func (l *upLogger) Print(level string, format string, msg ...interface{}) { - place := findPlace() - datetime := fmt.Sprintf("%s", time.Now())[0:19] - fmt.Println(level, datetime, msg, place) + Print(level, format, msg...) eventMiddle := event.NewEvent(middleMsg.EventNameLog) eventMiddle.SetValue(middleMsg.EventKeyLog, middleMsg.LogContext{}) - l.produce.Call(ctx, eventMiddle) + l.produce.Call(l.ctx, eventMiddle) } -func Debug(msg interface{}) { - Print("DEBUG", msg) +func Debug(format string, msg ...interface{}) { + Print("DEBUG", format, msg...) } -func Info(msg interface{}) { - Print("INFO", msg) +func Info(format string, msg ...interface{}) { + Print("INFO", format, msg...) } -func Warn(msg interface{}) { - Print("WARN", msg) +func Warn(format string, msg ...interface{}) { + Print("WARN", format, msg...) } -func Error(msg interface{}) { - Print("ERROR", msg) +func Error(format string, msg ...interface{}) { + Print("ERROR", format, msg...) } -func Print(level string, msg interface{}) { +func Panic(format string, msg ...interface{}) { + Print("ERROR", format, msg...) +} +func Print(level string, format string, msg ...interface{}) { place := findPlace() - datetime := fmt.Sprintf("%s", time.Now())[0:19] - fmt.Println(level, datetime, msg, place) + fmt.Println(level, datetime, fmt.Sprintf(format, msg...)) + + for _, lv := range stath { + if strings.ToUpper(lv) == strings.ToUpper(level) { + fmt.Println(place) + break + } + } } func findPlace() string { + var ( place string i = 0