From 51517da519f7e2e754a621e5ff3b36ea76cbf3f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=8E=E7=99=BD=E5=8D=97?= <1762386214@qq.com> Date: Sun, 10 Oct 2021 21:06:28 +0800 Subject: [PATCH 1/2] feat(logx) add print without format --- pkg/logx/define.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/logx/define.go b/pkg/logx/define.go index e7e2122..d274e3b 100644 --- a/pkg/logx/define.go +++ b/pkg/logx/define.go @@ -32,5 +32,11 @@ type logInterface interface { Error(format string, msg ...interface{}) Panic(format string, msg ...interface{}) + Debugln(msg ...interface{}) + Infoln(msg ...interface{}) + Warnln(msg ...interface{}) + Errorln(msg ...interface{}) + Panicln(msg ...interface{}) + Print(level string, format string, msg ...interface{}) } From e5ac4d1cf90b7167dec9c0eadc0b8790d36b95cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=8E=E7=99=BD=E5=8D=97?= <1762386214@qq.com> Date: Sun, 10 Oct 2021 21:06:55 +0800 Subject: [PATCH 2/2] feat(logx) add print without format, color print --- pkg/logx/logx.go | 108 ++++++++++++++++++++++++++++++++---------- pkg/logx/logx_test.go | 15 +++++- 2 files changed, 95 insertions(+), 28 deletions(-) diff --git a/pkg/logx/logx.go b/pkg/logx/logx.go index 18b40d0..3814ebe 100644 --- a/pkg/logx/logx.go +++ b/pkg/logx/logx.go @@ -11,12 +11,7 @@ import ( "time" ) -var ( - floor = 3 -) - func With(ctx context.Context, p event.ProduceInterface) *upLogger { - floor = 4 return &upLogger{ ctx: ctx, produce: p, @@ -26,57 +21,110 @@ func With(ctx context.Context, p event.ProduceInterface) *upLogger { func (l *upLogger) Debug(format string, msg ...interface{}) { l.Print("DEBUG", format, msg...) } + func (l *upLogger) Info(format string, msg ...interface{}) { l.Print("INFO", format, msg...) } + func (l *upLogger) Warn(format string, msg ...interface{}) { l.Print("WARN", format, 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...) + l.Print("ERROR", format, msg...) os.Exit(-1) } func (l *upLogger) Print(level string, format string, msg ...interface{}) { - - Print(level, format, msg...) + logPrint(4, level, format, msg...) eventMiddle := event.NewEvent(middleMsg.EventNameLog) eventMiddle.SetValue(middleMsg.MiddleMsgKey, middleMsg.LogContext{ Level: level, Data: time.Now(), Msg: fmt.Sprintf(format, msg...), - Route: findPlace(), + Route: findPlace(4), }) l.produce.Call(l.ctx, eventMiddle) } -func Debug(format string, msg ...interface{}) { - Print("DEBUG", format, msg...) -} -func Info(format string, msg ...interface{}) { - Print("INFO", format, msg...) -} -func Warn(format string, msg ...interface{}) { - Print("WARN", format, msg...) -} -func Error(format string, msg ...interface{}) { - Print("ERROR", format, msg...) +func (l *upLogger) Debugln(msg ...interface{}) { + l.Print("DEBUG", "%s", format(msg...)) } -func Panic(format string, msg ...interface{}) { - Print("ERROR", format, msg...) +func (l *upLogger) Infoln(msg ...interface{}) { + l.Print("INFO", "%s", format(msg...)) +} + +func (l *upLogger) Warnln(msg ...interface{}) { + l.Print("WARN", "%s", format(msg...)) +} + +func (l *upLogger) Errorln(msg ...interface{}) { + l.Print("ERROR", "%s", format(msg...)) +} + +func (l *upLogger) Panicln(msg ...interface{}) { + l.Print("ERROR", "%s", format(msg...)) os.Exit(-1) } -func Print(level string, format string, msg ...interface{}) { - place := findPlace() +func Debug(format string, msg ...interface{}) { + logPrint(3, "DEBUG", format, msg...) +} +func Info(format string, msg ...interface{}) { + logPrint(3, "INFO", format, msg...) +} +func Warn(format string, msg ...interface{}) { + logPrint(3, "WARN", format, msg...) +} +func Error(format string, msg ...interface{}) { + logPrint(3, "ERROR", format, msg...) +} +func Panic(format string, msg ...interface{}) { + logPrint(3, "PANIC", format, msg...) + os.Exit(-1) +} + +func Debugln(msg ...interface{}) { + logPrint(3, "DEBUG", "%s", format(msg...)) +} +func Infoln(msg ...interface{}) { + logPrint(3, "INFO", "%s", format(msg...)) +} +func Warnln(msg ...interface{}) { + logPrint(3, "WARN", "%s", format(msg...)) +} +func Errorln(msg ...interface{}) { + logPrint(3, "ERROR", "%s", format(msg...)) +} +func Panicln(msg ...interface{}) { + logPrint(3, "PANIC", "%s", format(msg...)) + os.Exit(-1) +} + +func logPrint(floor int, level string, format string, msg ...interface{}) { + place := findPlace(floor) datetime := fmt.Sprintf("%s", time.Now())[0:19] - fmt.Println(level, datetime, fmt.Sprintf(format, msg...)) + switch level { + case "DEBUG": + fmt.Printf("\033[1;37;40m") + case "INFO": + fmt.Printf("\033[1;32;40m") + case "WARN": + fmt.Printf("\033[1;33;40m") + default: + fmt.Printf("\033[1;31;40m") + + } + + //fmt.Println(level, datetime, fmt.Sprintf(format, msg...)) + fmt.Printf("%s\t%v\t%s\n", level, datetime, fmt.Sprintf(format, msg...)) for _, lv := range stath { if strings.ToUpper(lv) == strings.ToUpper(level) { @@ -86,7 +134,7 @@ func Print(level string, format string, msg ...interface{}) { } } -func findPlace() string { +func findPlace(floor int) string { var ( place string @@ -104,3 +152,11 @@ func findPlace() string { return place } + +func format(message ...interface{}) (context string) { + for _, msg := range message { + context = fmt.Sprintf("%s\t%v", context, msg) + } + + return context +} diff --git a/pkg/logx/logx_test.go b/pkg/logx/logx_test.go index c273047..a1f0b4b 100644 --- a/pkg/logx/logx_test.go +++ b/pkg/logx/logx_test.go @@ -12,7 +12,13 @@ func TestStd(t *testing.T) { Debug("%d%s", 11, "Debug") Warn("%d%s", 11, "Warn") Error("%d%s", 11, "Error") - Panic("%d%s", 11, "Panic") + + Infoln(1321, "dwad", 0x9812933) + Debugln(1321, "dwad", 0x9812933) + Warnln(1321, "dwad", 0x9812933) + Errorln(1321, "dwad", 0x9812933) + + //Panic("%d%s", 11, "Panic") logger := With(context.Background(), event.NewProduce(event.NewDriver(100))) @@ -21,6 +27,11 @@ func TestStd(t *testing.T) { logger.Debug("%d%s", 11, "Debug") logger.Warn("%d%s", 11, "Warn") logger.Error("%d%s", 11, "Error") - logger.Panic("%d%s", 11, "Panic") + //logger.Panic("%d%s", 11, "Panic") + + logger.Infoln(11, "Info") + logger.Debugln(11, "Debug") + logger.Warnln(11, "Warn") + logger.Errorln(11, "Error") }