feat(logx) add log function

This commit is contained in:
黎白南 2021-10-05 20:52:45 +08:00
parent 5292163b14
commit a706d57370
5 changed files with 111 additions and 2 deletions

View File

@ -103,7 +103,7 @@
- 重做
```sh
git log
git logx
# 7e8108bc7347ef6e2f01d6bc1d1f249afc82a486
git reset 7e8108bc7347ef6e2f01d6bc1d1f249afc82a486

View File

@ -1 +0,0 @@
package log

39
pkg/logx/define.go Normal file
View File

@ -0,0 +1,39 @@
package logx
import (
"gitee.com/timedb/wheatCache/pkg/event"
"time"
)
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
Debug(msg interface{})
Info(msg interface{})
Warn(msg interface{})
Error(msg interface{})
Print(level string, msg interface{})
}
func NewLogger() *logger {
return &logger{}
}

60
pkg/logx/logx.go Normal file
View File

@ -0,0 +1,60 @@
package logx
import (
"fmt"
"gitee.com/timedb/wheatCache/pkg/event"
"runtime"
"time"
)
type logContext struct {
level string
dataStr string
msg string
route string
}
func (l *logger) With(p *event.ProduceInterface) *upLogger {
return &upLogger{}
}
func (l *logger) Print(level string, msg interface{}) {
var (
place string
i = 0
)
for {
_, file, line, _ := runtime.Caller(i)
if line == 0 {
break
}
i++
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)
}

11
pkg/logx/logx_test.go Normal file
View File

@ -0,0 +1,11 @@
package logx
import (
"gitee.com/timedb/wheatCache/pkg/event"
"testing"
)
func TestStd(t *testing.T) {
produce := event.NewProduce(nil)
NewLogger().With(&produce).Info(111)
}