wheat-cache/pkg/logx/logx.go

102 lines
2.1 KiB
Go
Raw Normal View History

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-06 19:14:16 +08:00
"os"
2021-10-05 20:52:45 +08:00
"runtime"
2021-10-06 19:00:48 +08:00
"strings"
2021-10-05 20:52:45 +08:00
"time"
)
2021-10-06 19:00:48 +08:00
func With(ctx context.Context, p event.ProduceInterface) *upLogger {
2021-10-05 21:36:06 +08:00
return &upLogger{
2021-10-06 19:00:48 +08:00
ctx: ctx,
2021-10-06 14:47:58 +08:00
produce: p,
2021-10-05 21:36:06 +08:00
}
}
2021-10-06 19:00:48 +08:00
func (l *upLogger) Debug(format string, msg ...interface{}) {
l.Print("DEBUG", format, msg...)
2021-10-05 21:36:06 +08:00
}
2021-10-06 19:00:48 +08:00
func (l *upLogger) Info(format string, msg ...interface{}) {
l.Print("INFO", format, msg...)
2021-10-05 21:36:06 +08:00
}
2021-10-06 19:00:48 +08:00
func (l *upLogger) Warn(format string, msg ...interface{}) {
l.Print("WARN", format, msg...)
2021-10-05 21:36:06 +08:00
}
2021-10-06 19:00:48 +08:00
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...)
2021-10-06 19:14:16 +08:00
os.Exit(-1)
2021-10-05 21:36:06 +08:00
}
2021-10-06 19:00:48 +08:00
func (l *upLogger) Print(level string, format string, msg ...interface{}) {
2021-10-05 21:36:06 +08:00
2021-10-06 19:00:48 +08:00
Print(level, format, msg...)
2021-10-05 21:36:06 +08:00
2021-10-06 14:47:58 +08:00
eventMiddle := event.NewEvent(middleMsg.EventNameLog)
eventMiddle.SetValue(middleMsg.EventKeyLog, middleMsg.LogContext{
Level: level,
Data: time.Now(),
Msg: fmt.Sprintf(format, msg...),
Route: findPlace(),
})
2021-10-06 19:00:48 +08:00
l.produce.Call(l.ctx, eventMiddle)
2021-10-05 20:52:45 +08:00
}
2021-10-06 19:00:48 +08:00
func Debug(format string, msg ...interface{}) {
Print("DEBUG", format, msg...)
2021-10-05 21:36:06 +08:00
}
2021-10-06 19:00:48 +08:00
func Info(format string, msg ...interface{}) {
Print("INFO", format, msg...)
2021-10-05 21:36:06 +08:00
}
2021-10-06 19:00:48 +08:00
func Warn(format string, msg ...interface{}) {
Print("WARN", format, msg...)
2021-10-05 21:36:06 +08:00
}
2021-10-06 19:00:48 +08:00
func Error(format string, msg ...interface{}) {
Print("ERROR", format, msg...)
2021-10-05 20:52:45 +08:00
}
2021-10-06 19:00:48 +08:00
func Panic(format string, msg ...interface{}) {
Print("ERROR", format, msg...)
2021-10-06 19:14:16 +08:00
os.Exit(-1)
2021-10-06 19:00:48 +08:00
}
2021-10-05 20:52:45 +08:00
2021-10-06 19:00:48 +08:00
func Print(level string, format string, msg ...interface{}) {
2021-10-05 21:36:06 +08:00
place := findPlace()
datetime := fmt.Sprintf("%s", time.Now())[0:19]
2021-10-06 19:00:48 +08:00
fmt.Println(level, datetime, fmt.Sprintf(format, msg...))
for _, lv := range stath {
if strings.ToUpper(lv) == strings.ToUpper(level) {
fmt.Println(place)
break
}
}
2021-10-05 21:36:06 +08:00
}
func findPlace() string {
2021-10-06 19:00:48 +08:00
2021-10-05 20:52:45 +08:00
var (
place string
i = 3
2021-10-05 20:52:45 +08:00
)
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
}