Merge pull request !50 from 黎白南/feat-logx-middle
This commit is contained in:
bandl 2021-10-06 11:14:52 +00:00 committed by Gitee
commit 63bd44da44
6 changed files with 180 additions and 1 deletions

View File

@ -13,3 +13,9 @@ lruCache:
maxSize: "1GB"
eventDriverSize: 2000
workTime: 1
logPrint:
stath: [
"debug",
"error"
]

View File

@ -1 +0,0 @@
package log

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

@ -0,0 +1,36 @@
package logx
import (
"context"
"gitee.com/timedb/wheatCache/pkg/event"
"github.com/spf13/viper"
"sync"
)
type LogLevelState int8
var (
once sync.Once
stath []string
)
type upLogger struct {
ctx context.Context
produce event.ProduceInterface
}
func init() {
once.Do(func() {
stath = viper.GetStringSlice("logPrint.stath")
})
}
type logInterface interface {
Debug(format string, msg ...interface{})
Info(format string, msg ...interface{})
Warn(format string, msg ...interface{})
Error(format string, msg ...interface{})
Panic(format string, msg ...interface{})
Print(level string, format string, msg ...interface{})
}

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

@ -0,0 +1,96 @@
package logx
import (
"context"
"fmt"
"gitee.com/timedb/wheatCache/pkg/event"
middleMsg "gitee.com/timedb/wheatCache/pkg/middle-msg"
"os"
"runtime"
"strings"
"time"
)
func With(ctx context.Context, p event.ProduceInterface) *upLogger {
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{})
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 = 0
)
for {
_, file, line, _ := runtime.Caller(i)
if line == 0 {
break
}
i++
place = fmt.Sprintf("%s:%d\n%s", file, line, place)
}
return place
}

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

@ -0,0 +1,26 @@
package logx
import (
"context"
_ "gitee.com/timedb/wheatCache/conf"
"gitee.com/timedb/wheatCache/pkg/event"
"testing"
)
func TestStd(t *testing.T) {
Info("%d%s", 11, "Info")
Debug("%d%s", 11, "Debug")
Warn("%d%s", 11, "Warn")
Error("%d%s", 11, "Error")
Panic("%d%s", 11, "Panic")
logger := With(context.Background(),
event.NewProduce(event.NewDriver(100)))
logger.Info("%d%s", 11, "Info")
logger.Debug("%d%s", 11, "Debug")
logger.Warn("%d%s", 11, "Warn")
logger.Error("%d%s", 11, "Error")
logger.Panic("%d%s", 11, "Panic")
}

16
pkg/middle-msg/logx.go Normal file
View File

@ -0,0 +1,16 @@
package middle_msg
import "time"
var (
EventNameLog = "LogContext"
EventKeyLog = "LogContext"
)
type LogContext struct {
Level string
Data time.Time
Msg string
Route string
}