Merge pull request !60 from 黎白南/feat-logx-middle
This commit is contained in:
bandl 2021-10-10 13:23:43 +00:00 committed by Gitee
commit ab257b2009
3 changed files with 101 additions and 28 deletions

View File

@ -32,5 +32,11 @@ type logInterface interface {
Error(format string, msg ...interface{}) Error(format string, msg ...interface{})
Panic(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{}) Print(level string, format string, msg ...interface{})
} }

View File

@ -11,12 +11,7 @@ import (
"time" "time"
) )
var (
floor = 3
)
func With(ctx context.Context, p event.ProduceInterface) *upLogger { func With(ctx context.Context, p event.ProduceInterface) *upLogger {
floor = 4
return &upLogger{ return &upLogger{
ctx: ctx, ctx: ctx,
produce: p, produce: p,
@ -26,57 +21,110 @@ func With(ctx context.Context, p event.ProduceInterface) *upLogger {
func (l *upLogger) Debug(format string, msg ...interface{}) { func (l *upLogger) Debug(format string, msg ...interface{}) {
l.Print("DEBUG", format, msg...) l.Print("DEBUG", format, msg...)
} }
func (l *upLogger) Info(format string, msg ...interface{}) { func (l *upLogger) Info(format string, msg ...interface{}) {
l.Print("INFO", format, msg...) l.Print("INFO", format, msg...)
} }
func (l *upLogger) Warn(format string, msg ...interface{}) { func (l *upLogger) Warn(format string, msg ...interface{}) {
l.Print("WARN", format, msg...) l.Print("WARN", format, msg...)
} }
func (l *upLogger) Error(format string, msg ...interface{}) { func (l *upLogger) Error(format string, msg ...interface{}) {
l.Print("ERROR", format, msg...) l.Print("ERROR", format, msg...)
} }
func (l *upLogger) Panic(format string, msg ...interface{}) { func (l *upLogger) Panic(format string, msg ...interface{}) {
Print("ERROR", format, msg...) l.Print("ERROR", format, msg...)
os.Exit(-1) os.Exit(-1)
} }
func (l *upLogger) Print(level string, format string, msg ...interface{}) { func (l *upLogger) Print(level string, format string, msg ...interface{}) {
logPrint(4, level, format, msg...)
Print(level, format, msg...)
eventMiddle := event.NewEvent(middleMsg.EventNameLog) eventMiddle := event.NewEvent(middleMsg.EventNameLog)
eventMiddle.SetValue(middleMsg.MiddleMsgKey, middleMsg.LogContext{ eventMiddle.SetValue(middleMsg.MiddleMsgKey, middleMsg.LogContext{
Level: level, Level: level,
Data: time.Now(), Data: time.Now(),
Msg: fmt.Sprintf(format, msg...), Msg: fmt.Sprintf(format, msg...),
Route: findPlace(), Route: findPlace(4),
}) })
l.produce.Call(l.ctx, eventMiddle) l.produce.Call(l.ctx, eventMiddle)
} }
func Debug(format string, msg ...interface{}) { func (l *upLogger) Debugln(msg ...interface{}) {
Print("DEBUG", format, msg...) l.Print("DEBUG", "%s", 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 Panic(format string, msg ...interface{}) { func (l *upLogger) Infoln(msg ...interface{}) {
Print("ERROR", format, msg...) 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) os.Exit(-1)
} }
func Print(level string, format string, msg ...interface{}) { func Debug(format string, msg ...interface{}) {
place := findPlace() 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] 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 { for _, lv := range stath {
if strings.ToUpper(lv) == strings.ToUpper(level) { 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 ( var (
place string place string
@ -104,3 +152,11 @@ func findPlace() string {
return place return place
} }
func format(message ...interface{}) (context string) {
for _, msg := range message {
context = fmt.Sprintf("%s\t%v", context, msg)
}
return context
}

View File

@ -12,7 +12,13 @@ func TestStd(t *testing.T) {
Debug("%d%s", 11, "Debug") Debug("%d%s", 11, "Debug")
Warn("%d%s", 11, "Warn") Warn("%d%s", 11, "Warn")
Error("%d%s", 11, "Error") 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(), logger := With(context.Background(),
event.NewProduce(event.NewDriver(100))) event.NewProduce(event.NewDriver(100)))
@ -21,6 +27,11 @@ func TestStd(t *testing.T) {
logger.Debug("%d%s", 11, "Debug") logger.Debug("%d%s", 11, "Debug")
logger.Warn("%d%s", 11, "Warn") logger.Warn("%d%s", 11, "Warn")
logger.Error("%d%s", 11, "Error") 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")
} }