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

View File

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