wheat-cache/pkg/logx/logx.go

107 lines
2.1 KiB
Go

package logx
import (
"context"
"fmt"
"gitee.com/timedb/wheatCache/pkg/event"
middleMsg "gitee.com/timedb/wheatCache/pkg/middle-msg"
"os"
"runtime"
"strings"
"time"
)
var (
floor = 3
)
func With(ctx context.Context, p event.ProduceInterface) *upLogger {
floor = 4
return &upLogger{
ctx: ctx,
produce: p,
}
}
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...)
os.Exit(-1)
}
func (l *upLogger) Print(level string, format string, msg ...interface{}) {
Print(level, format, msg...)
eventMiddle := event.NewEvent(middleMsg.EventNameLog)
eventMiddle.SetValue(middleMsg.EventKeyLog, middleMsg.LogContext{
Level: level,
Data: time.Now(),
Msg: fmt.Sprintf(format, msg...),
Route: findPlace(),
})
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 Panic(format string, msg ...interface{}) {
Print("ERROR", format, msg...)
os.Exit(-1)
}
func Print(level string, format string, msg ...interface{}) {
place := findPlace()
datetime := fmt.Sprintf("%s", time.Now())[0:19]
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 = floor
)
for {
_, file, line, _ := runtime.Caller(i)
if line == 0 {
break
}
i++
place = fmt.Sprintf("%s:%d\n%s", file, line, place)
}
return place
}