feat(logx) make struct without new

This commit is contained in:
黎白南 2021-10-05 21:36:06 +08:00
parent a706d57370
commit b75aa1d141
2 changed files with 62 additions and 54 deletions

View File

@ -2,38 +2,24 @@ package logx
import ( import (
"gitee.com/timedb/wheatCache/pkg/event" "gitee.com/timedb/wheatCache/pkg/event"
"time" middle_msg "gitee.com/timedb/wheatCache/pkg/middle-msg"
) )
type LogLevelState int8 type LogLevelState int8
const (
DebugState = LogLevelState(iota)
)
type logger struct {
Stack string
Msg string
Level LogLevelState
LogTime time.Time
}
type upLogger struct { type upLogger struct {
logger
produce *event.ProduceInterface produce *event.ProduceInterface
} }
type logFunc interface { type logInterface interface {
With(p *event.ProduceInterface) *upLogger 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(msg interface{}) Print(level string, msg interface{}) *middle_msg.LogContext
Info(msg interface{})
Warn(msg interface{})
Error(msg interface{})
Print(level string, msg interface{})
} }
func NewLogger() *logger { func NewLogger() *upLogger {
return &logger{} return &upLogger{}
} }

View File

@ -3,23 +3,67 @@ package logx
import ( import (
"fmt" "fmt"
"gitee.com/timedb/wheatCache/pkg/event" "gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/pkg/middle-msg"
"runtime" "runtime"
"time" "time"
) )
type logContext struct { func With(p *event.ProduceInterface) *upLogger {
level string return &upLogger{
dataStr string p,
msg string }
route string
} }
func (l *logger) With(p *event.ProduceInterface) *upLogger { func (l *upLogger) Debug(msg interface{}) *middle_msg.LogContext {
return &upLogger{} return l.Print("DEBUG", msg)
}
func (l *upLogger) Info(msg interface{}) *middle_msg.LogContext {
return l.Print("INFO", msg)
}
func (l *upLogger) Warn(msg interface{}) *middle_msg.LogContext {
return l.Print("WARN", msg)
}
func (l *upLogger) Error(msg interface{}) *middle_msg.LogContext {
return l.Print("ERROR", msg)
} }
func (l *logger) Print(level string, msg interface{}) { func (l *upLogger) Print(level string, msg interface{}) *middle_msg.LogContext {
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,
}
}
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)
}
func Print(level string, msg interface{}) {
place := findPlace()
datetime := fmt.Sprintf("%s", time.Now())[0:19]
fmt.Println(level, datetime, msg, place)
}
func findPlace() string {
var ( var (
place string place string
i = 0 i = 0
@ -34,27 +78,5 @@ func (l *logger) Print(level string, msg interface{}) {
place = fmt.Sprintf("%s:%d\n%s", file, line, place) place = fmt.Sprintf("%s:%d\n%s", file, line, place)
} }
datetime := fmt.Sprintf("%s", time.Now())[0:19] return place
fmt.Println(level, datetime, msg, place)
//_ = &logContext{
// level: level,
// dataStr: datetime,
// msg: fmt.Sprintf("%v", msg),
// route: place,
//}
}
func (l *logger) Debug(msg interface{}) {
l.Print("DEBUG", msg)
}
func (l *logger) Info(msg interface{}) {
l.Print("INFO", msg)
}
func (l *logger) Warn(msg interface{}) {
l.Print("WARN", msg)
}
func (l *logger) Error(msg interface{}) {
l.Print("ERROR", msg)
} }