2021-10-05 20:52:45 +08:00
|
|
|
package logx
|
|
|
|
|
|
|
|
import (
|
2021-10-06 14:47:58 +08:00
|
|
|
"context"
|
2021-10-05 20:52:45 +08:00
|
|
|
"fmt"
|
|
|
|
"gitee.com/timedb/wheatCache/pkg/event"
|
2021-10-06 14:47:58 +08:00
|
|
|
middleMsg "gitee.com/timedb/wheatCache/pkg/middle-msg"
|
2021-10-05 20:52:45 +08:00
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-10-06 14:47:58 +08:00
|
|
|
func With(p event.ProduceInterface) *upLogger {
|
2021-10-05 21:36:06 +08:00
|
|
|
return &upLogger{
|
2021-10-06 14:47:58 +08:00
|
|
|
produce: p,
|
2021-10-05 21:36:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 14:47:58 +08:00
|
|
|
func (l *upLogger) Debug(ctx context.Context, msg interface{}) {
|
|
|
|
l.Print("DEBUG", ctx, msg)
|
2021-10-05 21:36:06 +08:00
|
|
|
}
|
2021-10-06 14:47:58 +08:00
|
|
|
func (l *upLogger) Info(ctx context.Context, msg interface{}) {
|
|
|
|
l.Print("INFO", ctx, msg)
|
2021-10-05 21:36:06 +08:00
|
|
|
}
|
2021-10-06 14:47:58 +08:00
|
|
|
func (l *upLogger) Warn(ctx context.Context, msg interface{}) {
|
|
|
|
l.Print("WARN", ctx, msg)
|
2021-10-05 21:36:06 +08:00
|
|
|
}
|
2021-10-06 14:47:58 +08:00
|
|
|
func (l *upLogger) Error(ctx context.Context, msg interface{}) {
|
|
|
|
l.Print("ERROR", ctx, msg)
|
2021-10-05 21:36:06 +08:00
|
|
|
}
|
|
|
|
|
2021-10-06 14:47:58 +08:00
|
|
|
func (l *upLogger) Print(level string, ctx context.Context, msg interface{}) {
|
2021-10-05 21:36:06 +08:00
|
|
|
|
|
|
|
place := findPlace()
|
|
|
|
datetime := fmt.Sprintf("%s", time.Now())[0:19]
|
|
|
|
fmt.Println(level, datetime, msg, place)
|
|
|
|
|
2021-10-06 14:47:58 +08:00
|
|
|
eventMiddle := event.NewEvent(middleMsg.EventNameLog)
|
|
|
|
eventMiddle.SetValue(middleMsg.EventKeyLog, middleMsg.LogContext{})
|
|
|
|
l.produce.Call(ctx, eventMiddle)
|
2021-10-05 20:52:45 +08:00
|
|
|
}
|
|
|
|
|
2021-10-05 21:36:06 +08:00
|
|
|
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)
|
2021-10-05 20:52:45 +08:00
|
|
|
}
|
|
|
|
|
2021-10-05 21:36:06 +08:00
|
|
|
func Print(level string, msg interface{}) {
|
2021-10-05 20:52:45 +08:00
|
|
|
|
2021-10-05 21:36:06 +08:00
|
|
|
place := findPlace()
|
|
|
|
|
|
|
|
datetime := fmt.Sprintf("%s", time.Now())[0:19]
|
|
|
|
|
|
|
|
fmt.Println(level, datetime, msg, place)
|
|
|
|
}
|
|
|
|
|
|
|
|
func findPlace() string {
|
2021-10-05 20:52:45 +08:00
|
|
|
var (
|
|
|
|
place string
|
|
|
|
i = 0
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
_, file, line, _ := runtime.Caller(i)
|
|
|
|
if line == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
place = fmt.Sprintf("%s:%d\n%s", file, line, place)
|
|
|
|
}
|
|
|
|
|
2021-10-05 21:36:06 +08:00
|
|
|
return place
|
2021-10-05 20:52:45 +08:00
|
|
|
}
|