fix(log) fix log with context

This commit is contained in:
黎白南 2021-10-06 14:47:58 +08:00
parent 5061b2e3da
commit 0e2db14d32
3 changed files with 29 additions and 29 deletions

View File

@ -1,25 +1,21 @@
package logx
import (
"context"
"gitee.com/timedb/wheatCache/pkg/event"
middle_msg "gitee.com/timedb/wheatCache/pkg/middle-msg"
)
type LogLevelState int8
type upLogger struct {
produce *event.ProduceInterface
produce event.ProduceInterface
}
type logInterface interface {
Debug(msg interface{}) *middle_msg.LogContext
Info(msg interface{}) *middle_msg.LogContext
Warn(msg interface{}) *middle_msg.LogContext
Error(msg interface{}) *middle_msg.LogContext
Debug(ctx context.Context, msg interface{})
Info(ctx context.Context, msg interface{})
Warn(ctx context.Context, msg interface{})
Error(ctx context.Context, msg interface{})
Print(level string, msg interface{}) *middle_msg.LogContext
}
func NewLogger() *upLogger {
return &upLogger{}
Print(level string, ctx context.Context, msg interface{})
}

View File

@ -1,44 +1,42 @@
package logx
import (
"context"
"fmt"
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/pkg/middle-msg"
middleMsg "gitee.com/timedb/wheatCache/pkg/middle-msg"
"runtime"
"time"
)
func With(p *event.ProduceInterface) *upLogger {
func With(p event.ProduceInterface) *upLogger {
return &upLogger{
p,
produce: p,
}
}
func (l *upLogger) Debug(msg interface{}) *middle_msg.LogContext {
return l.Print("DEBUG", msg)
func (l *upLogger) Debug(ctx context.Context, msg interface{}) {
l.Print("DEBUG", ctx, msg)
}
func (l *upLogger) Info(msg interface{}) *middle_msg.LogContext {
return l.Print("INFO", msg)
func (l *upLogger) Info(ctx context.Context, msg interface{}) {
l.Print("INFO", ctx, msg)
}
func (l *upLogger) Warn(msg interface{}) *middle_msg.LogContext {
return l.Print("WARN", msg)
func (l *upLogger) Warn(ctx context.Context, msg interface{}) {
l.Print("WARN", ctx, msg)
}
func (l *upLogger) Error(msg interface{}) *middle_msg.LogContext {
return l.Print("ERROR", msg)
func (l *upLogger) Error(ctx context.Context, msg interface{}) {
l.Print("ERROR", ctx, msg)
}
func (l *upLogger) Print(level string, msg interface{}) *middle_msg.LogContext {
func (l *upLogger) Print(level string, ctx context.Context, msg interface{}) {
place := findPlace()
datetime := fmt.Sprintf("%s", time.Now())[0:19]
fmt.Println(level, datetime, msg, place)
return &middle_msg.LogContext{
Level: level,
Data: time.Now(),
Msg: fmt.Sprintf("%v", msg),
Route: place,
}
eventMiddle := event.NewEvent(middleMsg.EventNameLog)
eventMiddle.SetValue(middleMsg.EventKeyLog, middleMsg.LogContext{})
l.produce.Call(ctx, eventMiddle)
}
func Debug(msg interface{}) {

View File

@ -2,6 +2,12 @@ package middle_msg
import "time"
var (
EventNameLog = "LogContext"
EventKeyLog = "LogContext"
)
type LogContext struct {
Level string
Data time.Time