From c27e9d20ced3594c966ba0c4f1f3b7aadd268537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=8D=A3=E6=98=8C?= Date: Sun, 7 Apr 2024 12:02:02 +0800 Subject: [PATCH 1/7] =?UTF-8?q?fix:=20=E6=8A=BD=E8=B1=A1=20slog=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- log/kis_default_log.go | 182 ++++++++++++++++++++++++++++------------- 1 file changed, 126 insertions(+), 56 deletions(-) diff --git a/log/kis_default_log.go b/log/kis_default_log.go index 94da4ee..f6504a3 100644 --- a/log/kis_default_log.go +++ b/log/kis_default_log.go @@ -2,66 +2,136 @@ package log import ( "context" - "fmt" + "io" + "log/slog" + "os" + "path/filepath" "sync" ) -// kisDefaultLog 默认提供的日志对象 -type kisDefaultLog struct { - debugMode bool - mu sync.Mutex -} - -func (log *kisDefaultLog) SetDebugMode(enable bool) { - log.mu.Lock() - defer log.mu.Unlock() - log.debugMode = enable -} - -func (log *kisDefaultLog) InfoF(str string, v ...interface{}) { - fmt.Printf(str, v...) - fmt.Printf("\n") -} - -func (log *kisDefaultLog) ErrorF(str string, v ...interface{}) { - fmt.Printf(str, v...) - fmt.Printf("\n") -} - -func (log *kisDefaultLog) DebugF(str string, v ...interface{}) { - log.mu.Lock() - defer log.mu.Unlock() - if log.debugMode { - fmt.Printf(str, v...) - fmt.Printf("\n") - } -} - -func (log *kisDefaultLog) InfoFX(ctx context.Context, str string, v ...interface{}) { - fmt.Println(ctx) - fmt.Printf(str, v...) - fmt.Printf("\n") -} - -func (log *kisDefaultLog) ErrorFX(ctx context.Context, str string, v ...interface{}) { - fmt.Println(ctx) - fmt.Printf(str, v...) - fmt.Printf("\n") -} - -func (log *kisDefaultLog) DebugFX(ctx context.Context, str string, v ...interface{}) { - log.mu.Lock() - defer log.mu.Unlock() - if log.debugMode { - fmt.Println(ctx) - fmt.Printf(str, v...) - fmt.Printf("\n") - } -} - func init() { - // 如果没有设置Logger, 则启动时使用默认的kisDefaultLog对象 + // 如果没有设置 Logger, 则启动时使用默认的 kisDefaultSlog 对象 if Logger() == nil { - SetLogger(&kisDefaultLog{}) + MustNewKisDefaultSlog() } } + +func MustNewKisDefaultSlog(opts ...KisLogOptions) { + initDefaultSlog(loadKisDefaultLog(opts...)) + SetLogger(&kisDefaultSlog{}) +} + +// kisDefaultSlog 默认提供的日志对象 +type kisDefaultSlog struct { + location bool + level slog.Level + jsonFormat bool + writer io.Writer + + mu sync.Mutex +} + +type KisLogOptions func(k *kisDefaultSlog) + +func WithLocation(location bool) KisLogOptions { + return func(k *kisDefaultSlog) { + k.location = location + } +} + +func WithLevel(level slog.Level) KisLogOptions { + return func(k *kisDefaultSlog) { + k.level = level + } +} + +func WithJSONFormat(jsonFormat bool) KisLogOptions { + return func(k *kisDefaultSlog) { + k.jsonFormat = jsonFormat + } +} + +func WithWriter(writer io.Writer) KisLogOptions { + return func(k *kisDefaultSlog) { + k.writer = writer + } +} + +var defaultKisLog = &kisDefaultSlog{ + location: true, + level: slog.LevelDebug, + jsonFormat: false, + writer: os.Stdout, +} + +func loadKisDefaultLog(opts ...KisLogOptions) *kisDefaultSlog { + kisLog := defaultKisLog + if opts == nil { + return kisLog + } + + for _, opt := range opts { + opt(kisLog) + } + + return kisLog +} + +func (k *kisDefaultSlog) InfoFX(ctx context.Context, str string, v ...interface{}) { + slog.InfoContext(ctx, str, v) +} + +func (k *kisDefaultSlog) ErrorFX(ctx context.Context, str string, v ...interface{}) { + slog.ErrorContext(ctx, str, v) +} + +func (k *kisDefaultSlog) DebugFX(ctx context.Context, str string, v ...interface{}) { + slog.DebugContext(ctx, str, v) +} + +func (k *kisDefaultSlog) InfoF(str string, v ...interface{}) { + slog.Info(str, v) +} + +func (k *kisDefaultSlog) ErrorF(str string, v ...interface{}) { + slog.Error(str, v) +} + +func (k *kisDefaultSlog) DebugF(str string, v ...interface{}) { + slog.Debug(str, v) +} + +func (k *kisDefaultSlog) SetDebugMode(enable bool) { + k.mu.Lock() + defer k.mu.Unlock() + + if enable { + k.level = slog.LevelDebug + } +} + +func initDefaultSlog(kisLog *kisDefaultSlog) { + replace := func(groups []string, a slog.Attr) slog.Attr { + if a.Key == slog.SourceKey { + source := a.Value.Any().(*slog.Source) + source.Function = "" + source.File = filepath.Base(source.File) + } + + return a + } + + ho := &slog.HandlerOptions{ + AddSource: kisLog.location, + Level: kisLog.level, + ReplaceAttr: replace, + } + + var logger *slog.Logger + if kisLog.jsonFormat { + logger = slog.New(slog.NewJSONHandler(kisLog.writer, ho)) + } else { + logger = slog.New(slog.NewTextHandler(kisLog.writer, ho)) + } + slog.SetDefault(logger) +} From 233db23124773eec9cf337161ba471927b602121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=8D=A3=E6=98=8C?= Date: Sun, 7 Apr 2024 12:17:49 +0800 Subject: [PATCH 2/7] =?UTF-8?q?fix:=20=E6=95=B4=E7=90=86=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/kis_func_config.go | 2 +- flow/kis_flow.go | 8 ++++---- flow/kis_flow_data.go | 12 ++++++------ function/kis_function_c.go | 4 ++-- function/kis_function_e.go | 4 ++-- function/kis_function_l.go | 4 ++-- function/kis_function_s.go | 4 ++-- function/kis_function_v.go | 4 ++-- kis/pool.go | 20 ++++++++++---------- metrics/kis_metrics.go | 12 ++++++------ test/faas/faas_demo2.go | 4 ++-- test/kis_config_test.go | 8 ++++---- test/kis_flow_commit_batch_test.go | 2 +- 13 files changed, 44 insertions(+), 44 deletions(-) diff --git a/config/kis_func_config.go b/config/kis_func_config.go index 9421ebe..53dbde9 100644 --- a/config/kis_func_config.go +++ b/config/kis_func_config.go @@ -46,7 +46,7 @@ func NewFuncConfig( Name: "unNamedSource", } source = &defaultSource - log.Logger().InfoF("funcName NewConfig source is nil, funcName = %s, use default unNamed Source.", funcName) + log.Logger().InfoF("funcName NewConfig source is nil, use default unNamed Source.", "funcName", funcName) } config.Source = *source diff --git a/flow/kis_flow.go b/flow/kis_flow.go index 1ca0099..7e408f9 100644 --- a/flow/kis_flow.go +++ b/flow/kis_flow.go @@ -97,8 +97,8 @@ func (flow *KisFlow) Fork(ctx context.Context) kis.Flow { } } - log.Logger().DebugFX(ctx, "=====>Flow Fork, oldFlow.funcParams = %+v\n", flow.funcParams) - log.Logger().DebugFX(ctx, "=====>Flow Fork, newFlow.funcParams = %+v\n", newFlow.GetFuncParamsAllFuncs()) + log.Logger().DebugFX(ctx, "=====>Flow Fork, ", "oldFlow.funcParams", flow.funcParams) + log.Logger().DebugFX(ctx, "=====>Flow Fork, ", "newFlow.funcParams", newFlow.GetFuncParamsAllFuncs()) return newFlow } @@ -257,7 +257,7 @@ func (flow *KisFlow) Run(ctx context.Context) error { // 得到当前Function要处理与的源数据 if inputData, err := flow.getCurData(); err != nil { - log.Logger().ErrorFX(ctx, "flow.Run(): getCurData err = %s\n", err.Error()) + log.Logger().ErrorFX(ctx, "flow.Run(): getCurData", "err", err.Error()) return err } else { flow.inPut = inputData @@ -346,7 +346,7 @@ func (flow *KisFlow) GetFuncConfigByName(funcName string) *config.KisFuncConfig if f, ok := flow.Funcs[funcName]; ok { return f.GetConfig() } else { - log.Logger().ErrorF("GetFuncConfigByName(): Function %s not found", funcName) + log.Logger().ErrorF("GetFuncConfigByName(): Function not found", "FunctionName", funcName) return nil } } diff --git a/flow/kis_flow_data.go b/flow/kis_flow_data.go index df55d10..361966e 100644 --- a/flow/kis_flow_data.go +++ b/flow/kis_flow_data.go @@ -69,11 +69,11 @@ func (flow *KisFlow) commitSrcData(ctx context.Context) error { // 统计数据总量 Metrics.DataTota 指标累计加1 metrics.Metrics.DataTotal.Add(float64(dataCnt)) - //统计当前Flow数量指标 + // 统计当前Flow数量指标 metrics.Metrics.FlowDataTotal.WithLabelValues(flow.Name).Add(float64(dataCnt)) } - log.Logger().DebugFX(ctx, "====> After CommitSrcData, flow_name = %s, flow_id = %s\nAll Level Data =\n %+v\n", flow.Name, flow.Id, flow.data) + log.Logger().DebugFX(ctx, "====> After CommitSrcData", "flow_name", flow.Name, "flow_id", flow.Id, "All Level Data", flow.data) return nil } @@ -106,7 +106,7 @@ func (flow *KisFlow) commitReuseData(ctx context.Context) error { // 清空缓冲Buf (如果是ReuseData选项,那么提交的全部数据,都将不会携带到下一层) flow.buffer = flow.buffer[0:0] - log.Logger().DebugFX(ctx, " ====> After commitReuseData, flow_name = %s, flow_id = %s\nAll Level Data =\n %+v\n", flow.Name, flow.Id, flow.data) + log.Logger().DebugFX(ctx, " ====> After commitReuseData", "flow_name", flow.Name, "flow_id", flow.Id, "All Level Data", flow.data) return nil } @@ -122,12 +122,12 @@ func (flow *KisFlow) commitVoidData(ctx context.Context) error { // 将本层计算的缓冲数据提交到本层结果数据中 flow.data[flow.ThisFunctionId] = batch - log.Logger().DebugFX(ctx, " ====> After commitVoidData, flow_name = %s, flow_id = %s\nAll Level Data =\n %+v\n", flow.Name, flow.Id, flow.data) + log.Logger().DebugFX(ctx, " ====> After commitVoidData", "flow_name", flow.Name, "flow_id", flow.Id, "All Level Data", flow.data) return nil } -//commitCurData 提交Flow当前执行Function的结果数据 +// commitCurData 提交Flow当前执行Function的结果数据 func (flow *KisFlow) commitCurData(ctx context.Context) error { // 判断本层计算是否有结果数据,如果没有则退出本次Flow Run循环 @@ -150,7 +150,7 @@ func (flow *KisFlow) commitCurData(ctx context.Context) error { // 清空缓冲Buf flow.buffer = flow.buffer[0:0] - log.Logger().DebugFX(ctx, " ====> After commitCurData, flow_name = %s, flow_id = %s\nAll Level Data =\n %+v\n", flow.Name, flow.Id, flow.data) + log.Logger().DebugFX(ctx, " ====> After commitCurData", "flow_name", flow.Name, "flow_id", flow.Id, "All Level Data", flow.data) return nil } diff --git a/function/kis_function_c.go b/function/kis_function_c.go index 9da4b1e..3c3baab 100644 --- a/function/kis_function_c.go +++ b/function/kis_function_c.go @@ -20,11 +20,11 @@ func NewKisFunctionC() kis.Function { } func (f *KisFunctionC) Call(ctx context.Context, flow kis.Flow) error { - log.Logger().DebugF("KisFunctionC, flow = %+v\n", flow) + log.Logger().DebugF("KisFunctionC", "flow", flow) // 通过KisPool 路由到具体的执行计算Function中 if err := kis.Pool().CallFunction(ctx, f.Config.FName, flow); err != nil { - log.Logger().ErrorFX(ctx, "Function Called Error err = %s\n", err) + log.Logger().ErrorFX(ctx, "Function Called Error", "err", err) return err } diff --git a/function/kis_function_e.go b/function/kis_function_e.go index 5159aaa..2f8aace 100644 --- a/function/kis_function_e.go +++ b/function/kis_function_e.go @@ -20,11 +20,11 @@ func NewKisFunctionE() kis.Function { } func (f *KisFunctionE) Call(ctx context.Context, flow kis.Flow) error { - log.Logger().DebugF("KisFunctionE, flow = %+v\n", flow) + log.Logger().DebugF("KisFunctionE", "flow", flow) // 通过KisPool 路由到具体的执行计算Function中 if err := kis.Pool().CallFunction(ctx, f.Config.FName, flow); err != nil { - log.Logger().ErrorFX(ctx, "Function Called Error err = %s\n", err) + log.Logger().ErrorFX(ctx, "Function Called Error", "err", err) return err } diff --git a/function/kis_function_l.go b/function/kis_function_l.go index 24e87ac..ec6edb5 100644 --- a/function/kis_function_l.go +++ b/function/kis_function_l.go @@ -20,11 +20,11 @@ func NewKisFunctionL() kis.Function { } func (f *KisFunctionL) Call(ctx context.Context, flow kis.Flow) error { - log.Logger().DebugF("KisFunctionL, flow = %+v\n", flow) + log.Logger().DebugF("KisFunctionL", "flow", flow) // 通过KisPool 路由到具体的执行计算Function中 if err := kis.Pool().CallFunction(ctx, f.Config.FName, flow); err != nil { - log.Logger().ErrorFX(ctx, "Function Called Error err = %s\n", err) + log.Logger().ErrorFX(ctx, "Function Called Error", "err", err) return err } diff --git a/function/kis_function_s.go b/function/kis_function_s.go index d8e4ef5..5afdc30 100644 --- a/function/kis_function_s.go +++ b/function/kis_function_s.go @@ -20,11 +20,11 @@ func NewKisFunctionS() kis.Function { } func (f *KisFunctionS) Call(ctx context.Context, flow kis.Flow) error { - log.Logger().DebugF("KisFunctionS, flow = %+v\n", flow) + log.Logger().DebugF("KisFunctionS", "flow", flow) // 通过KisPool 路由到具体的执行计算Function中 if err := kis.Pool().CallFunction(ctx, f.Config.FName, flow); err != nil { - log.Logger().ErrorFX(ctx, "Function Called Error err = %s\n", err) + log.Logger().ErrorFX(ctx, "Function Called Error", "err", err) return err } diff --git a/function/kis_function_v.go b/function/kis_function_v.go index a661534..3e10b02 100644 --- a/function/kis_function_v.go +++ b/function/kis_function_v.go @@ -20,11 +20,11 @@ func NewKisFunctionV() kis.Function { } func (f *KisFunctionV) Call(ctx context.Context, flow kis.Flow) error { - log.Logger().DebugF("KisFunctionV, flow = %+v\n", flow) + log.Logger().DebugF("KisFunctionV", "flow", flow) // 通过KisPool 路由到具体的执行计算Function中 if err := kis.Pool().CallFunction(ctx, f.Config.FName, flow); err != nil { - log.Logger().ErrorFX(ctx, "Function Called Error err = %s\n", err) + log.Logger().ErrorFX(ctx, "Function Called Error", "err", err) return err } diff --git a/kis/pool.go b/kis/pool.go index 3607b80..bd42f95 100644 --- a/kis/pool.go +++ b/kis/pool.go @@ -33,7 +33,7 @@ var _pool *kisPool // Pool 单例构造 func Pool() *kisPool { _poolOnce.Do(func() { - //创建kisPool对象 + // 创建kisPool对象 _pool = new(kisPool) // fnRouter初始化 @@ -61,7 +61,7 @@ func (pool *kisPool) AddFlow(name string, flow Flow) { panic(errString) } - log.Logger().InfoF("Add FlowRouter FlowName=%s", name) + log.Logger().InfoF("Add FlowRouter", "FlowName", name) } func (pool *kisPool) GetFlow(name string) Flow { @@ -95,7 +95,7 @@ func (pool *kisPool) FaaS(fnName string, f FaaS) { panic(errString) } - log.Logger().InfoF("Add KisPool FuncName=%s", fnName) + log.Logger().InfoF("Add KisPool", "FuncName", fnName) } // CallFunction 调度 Function @@ -127,7 +127,7 @@ func (pool *kisPool) CallFunction(ctx context.Context, fnName string, flow Flow) // 将flow.Input()中的原始数据,反序列化为argType类型的数据 value, err := funcDesc.Serialize.UnMarshal(flow.Input(), argType) if err != nil { - log.Logger().ErrorFX(ctx, "funcDesc.Serialize.DecodeParam err=%v", err) + log.Logger().ErrorFX(ctx, "funcDesc.Serialize.DecodeParam", "err", err) } else { params = append(params, value) continue @@ -153,7 +153,7 @@ func (pool *kisPool) CallFunction(ctx context.Context, fnName string, flow Flow) } - log.Logger().ErrorFX(ctx, "FuncName: %s Can not find in KisPool, Not Added.\n", fnName) + log.Logger().ErrorFX(ctx, "FuncName: Can not find in KisPool, Not Added.", "FuncName", fnName) return errors.New("FuncName: " + fnName + " Can not find in NsPool, Not Added.") } @@ -170,7 +170,7 @@ func (pool *kisPool) CaaSInit(cname string, c ConnInit) { panic(errString) } - log.Logger().InfoF("Add KisPool CaaSInit CName=%s", cname) + log.Logger().InfoF("Add KisPool CaaSInit", "CName", cname) } // CallConnInit 调度 ConnInit @@ -193,10 +193,10 @@ func (pool *kisPool) CaaS(cname string, fname string, mode common.KisMode, c Caa defer pool.cLock.Unlock() if _, ok := pool.cTree[cname]; !ok { - //cid 首次注册,不存在,创建二级树NsConnSL + // cid 首次注册,不存在,创建二级树NsConnSL pool.cTree[cname] = make(connSL) - //初始化各类型FunctionMode + // 初始化各类型FunctionMode pool.cTree[cname][common.S] = make(connFuncRouter) pool.cTree[cname][common.L] = make(connFuncRouter) } @@ -208,7 +208,7 @@ func (pool *kisPool) CaaS(cname string, fname string, mode common.KisMode, c Caa panic(errString) } - log.Logger().InfoF("Add KisPool CaaS CName=%s, FName=%s, Mode =%s", cname, fname, mode) + log.Logger().InfoF("Add KisPool CaaS", "CName", cname, "FName", fname, "Mode", mode) } // CallConnector 调度 Connector @@ -223,7 +223,7 @@ func (pool *kisPool) CallConnector(ctx context.Context, flow Flow, conn Connecto return callback(ctx, conn, fn, flow, args) } - log.Logger().ErrorFX(ctx, "CName:%s FName:%s mode:%s Can not find in KisPool, Not Added.\n", conn.GetName(), fnConf.FName, mode) + log.Logger().ErrorFX(ctx, "Can not find in KisPool, Not Added.", "CName", conn.GetName(), " FName", fnConf.FName, "mode", mode) return nil, errors.New(fmt.Sprintf("CName:%s FName:%s mode:%s Can not find in KisPool, Not Added.", conn.GetName(), fnConf.FName, mode)) } diff --git a/metrics/kis_metrics.go b/metrics/kis_metrics.go index de8a525..248f909 100644 --- a/metrics/kis_metrics.go +++ b/metrics/kis_metrics.go @@ -34,9 +34,9 @@ func RunMetricsService(serverAddr string) error { http.Handle(common.METRICS_ROUTE, promhttp.Handler()) // 启动HttpServer - err := http.ListenAndServe(serverAddr, nil) //多个进程不可监听同一个端口 + err := http.ListenAndServe(serverAddr, nil) // 多个进程不可监听同一个端口 if err != nil { - log.Logger().ErrorF("RunMetricsService err = %s\n", err) + log.Logger().ErrorF("RunMetricsService", "err", err) } return err @@ -67,7 +67,7 @@ func InitMetrics() { Name: common.GANGE_FLOW_SCHE_CNTS_NAME, Help: common.GANGE_FLOW_SCHE_CNTS_HELP, }, - //标签名称 + // 标签名称 []string{common.LABEL_FLOW_NAME}, ) @@ -77,7 +77,7 @@ func InitMetrics() { Name: common.GANGE_FUNC_SCHE_CNTS_NAME, Help: common.GANGE_FUNC_SCHE_CNTS_HELP, }, - //标签名称 + // 标签名称 []string{common.LABEL_FUNCTION_NAME, common.LABEL_FUNCTION_MODE}, ) @@ -85,7 +85,7 @@ func InitMetrics() { Metrics.FunctionDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ Name: common.HISTOGRAM_FUNCTION_DURATION_NAME, Help: common.HISTOGRAM_FUNCTION_DURATION_HELP, - Buckets: []float64{0.005, 0.01, 0.03, 0.08, 0.1, 0.5, 1.0, 5.0, 10, 100, 1000, 5000, 30000}, //单位ms,最大半分钟 + Buckets: []float64{0.005, 0.01, 0.03, 0.08, 0.1, 0.5, 1.0, 5.0, 10, 100, 1000, 5000, 30000}, // 单位ms,最大半分钟 }, []string{common.LABEL_FUNCTION_NAME, common.LABEL_FUNCTION_MODE}, ) @@ -95,7 +95,7 @@ func InitMetrics() { prometheus.HistogramOpts{ Name: common.HISTOGRAM_FLOW_DURATION_NAME, Help: common.HISTOGRAM_FLOW_DURATION_HELP, - Buckets: []float64{0.005, 0.01, 0.03, 0.08, 0.1, 0.5, 1.0, 5.0, 10, 100, 1000, 5000, 30000, 60000}, //单位ms,最大1分钟 + Buckets: []float64{0.005, 0.01, 0.03, 0.08, 0.1, 0.5, 1.0, 5.0, 10, 100, 1000, 5000, 30000, 60000}, // 单位ms,最大1分钟 }, []string{common.LABEL_FLOW_NAME}, ) diff --git a/test/faas/faas_demo2.go b/test/faas/faas_demo2.go index e050e81..1929eb5 100644 --- a/test/faas/faas_demo2.go +++ b/test/faas/faas_demo2.go @@ -19,12 +19,12 @@ func FuncDemo2Handler(ctx context.Context, flow kis.Flow) error { conn, err := flow.GetConnector() if err != nil { - log.Logger().ErrorFX(ctx, "FuncDemo2Handler(): GetConnector err = %s\n", err.Error()) + log.Logger().ErrorFX(ctx, "FuncDemo2Handler(): GetConnector", "err", err.Error()) return err } if _, err := conn.Call(ctx, flow, row); err != nil { - log.Logger().ErrorFX(ctx, "FuncDemo2Handler(): Call err = %s\n", err.Error()) + log.Logger().ErrorFX(ctx, "FuncDemo2Handler(): Call", "err", err.Error()) return err } diff --git a/test/kis_config_test.go b/test/kis_config_test.go index d737968..550b3c4 100644 --- a/test/kis_config_test.go +++ b/test/kis_config_test.go @@ -26,7 +26,7 @@ func TestNewFuncConfig(t *testing.T) { myFunc1 := config.NewFuncConfig("funcName1", common.S, &source, &option) - log.Logger().InfoF("funcName1: %+v\n", myFunc1) + log.Logger().InfoF("funcName1", myFunc1) } func TestNewFlowConfig(t *testing.T) { @@ -50,7 +50,7 @@ func TestNewFlowConfig(t *testing.T) { myFlow1.AppendFunctionConfig(flowFuncParams1) myFlow1.AppendFunctionConfig(flowFuncParams2) - log.Logger().InfoF("myFlow1: %+v\n", myFlow1) + log.Logger().InfoF("myFlow1", myFlow1) } func TestNewConnConfig(t *testing.T) { @@ -81,8 +81,8 @@ func TestNewConnConfig(t *testing.T) { myConnector1 := config.NewConnConfig("connectorName1", "0.0.0.0:9987,0.0.0.0:9997", common.REDIS, "key", connParams) if err := myConnector1.WithFunc(myFunc1); err != nil { - log.Logger().ErrorF("WithFunc err: %s\n", err.Error()) + log.Logger().ErrorF("WithFunc", "err", err.Error()) } - log.Logger().InfoF("myConnector1: %+v\n", myConnector1) + log.Logger().InfoF("myConnector1", myConnector1) } diff --git a/test/kis_flow_commit_batch_test.go b/test/kis_flow_commit_batch_test.go index fb919f9..2ef4792 100644 --- a/test/kis_flow_commit_batch_test.go +++ b/test/kis_flow_commit_batch_test.go @@ -27,7 +27,7 @@ func TestForkFlowCommitBatch(t *testing.T) { // 3. 提交原始数据 if err := flow1.CommitRowBatch(stringRows); err != nil { - log.Logger().ErrorF("CommitRowBatch Error, err = %+v", err) + log.Logger().ErrorF("CommitRowBatch Error", "err", err) panic(err) } From cb83d7a04c4c1441a3adda81ded85e94f1ef0824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=8D=A3=E6=98=8C?= Date: Sun, 7 Apr 2024 12:20:48 +0800 Subject: [PATCH 3/7] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- log/kis_default_log.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/log/kis_default_log.go b/log/kis_default_log.go index f6504a3..671222f 100644 --- a/log/kis_default_log.go +++ b/log/kis_default_log.go @@ -78,27 +78,27 @@ func loadKisDefaultLog(opts ...KisLogOptions) *kisDefaultSlog { } func (k *kisDefaultSlog) InfoFX(ctx context.Context, str string, v ...interface{}) { - slog.InfoContext(ctx, str, v) + slog.InfoContext(ctx, str, v...) } func (k *kisDefaultSlog) ErrorFX(ctx context.Context, str string, v ...interface{}) { - slog.ErrorContext(ctx, str, v) + slog.ErrorContext(ctx, str, v...) } func (k *kisDefaultSlog) DebugFX(ctx context.Context, str string, v ...interface{}) { - slog.DebugContext(ctx, str, v) + slog.DebugContext(ctx, str, v...) } func (k *kisDefaultSlog) InfoF(str string, v ...interface{}) { - slog.Info(str, v) + slog.Info(str, v...) } func (k *kisDefaultSlog) ErrorF(str string, v ...interface{}) { - slog.Error(str, v) + slog.Error(str, v...) } func (k *kisDefaultSlog) DebugF(str string, v ...interface{}) { - slog.Debug(str, v) + slog.Debug(str, v...) } func (k *kisDefaultSlog) SetDebugMode(enable bool) { From 22729da9f5f7467ad19e4c3193edd07a990702f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=8D=A3=E6=98=8C?= Date: Sun, 7 Apr 2024 14:19:18 +0800 Subject: [PATCH 4/7] fix: rename default log --- log/kis_default_log.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/log/kis_default_log.go b/log/kis_default_log.go index 671222f..ac2c31a 100644 --- a/log/kis_default_log.go +++ b/log/kis_default_log.go @@ -17,8 +17,8 @@ func init() { } func MustNewKisDefaultSlog(opts ...KisLogOptions) { - initDefaultSlog(loadKisDefaultLog(opts...)) - SetLogger(&kisDefaultSlog{}) + defaultSlog := getKisDefaultSLog(opts...) + SetLogger(defaultSlog) } // kisDefaultSlog 默认提供的日志对象 @@ -64,17 +64,18 @@ var defaultKisLog = &kisDefaultSlog{ writer: os.Stdout, } -func loadKisDefaultLog(opts ...KisLogOptions) *kisDefaultSlog { - kisLog := defaultKisLog +func getKisDefaultSLog(opts ...KisLogOptions) *kisDefaultSlog { + defaultKisSlog := defaultKisLog if opts == nil { - return kisLog + return defaultKisSlog } for _, opt := range opts { - opt(kisLog) + opt(defaultKisSlog) } - return kisLog + initDefaultSlog(defaultKisSlog) + return defaultKisSlog } func (k *kisDefaultSlog) InfoFX(ctx context.Context, str string, v ...interface{}) { From 8e2f22fe521489a128ae7a86c8d1c735ffcda29e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=8D=A3=E6=98=8C?= Date: Sun, 7 Apr 2024 14:32:54 +0800 Subject: [PATCH 5/7] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=20setDebugMode=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- log/kis_default_log.go | 9 +++------ log/kis_log.go | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/log/kis_default_log.go b/log/kis_default_log.go index ac2c31a..a74eeab 100644 --- a/log/kis_default_log.go +++ b/log/kis_default_log.go @@ -18,6 +18,7 @@ func init() { func MustNewKisDefaultSlog(opts ...KisLogOptions) { defaultSlog := getKisDefaultSLog(opts...) + initDefaultSlog(defaultSlog) SetLogger(defaultSlog) } @@ -74,7 +75,6 @@ func getKisDefaultSLog(opts ...KisLogOptions) *kisDefaultSlog { opt(defaultKisSlog) } - initDefaultSlog(defaultKisSlog) return defaultKisSlog } @@ -102,13 +102,10 @@ func (k *kisDefaultSlog) DebugF(str string, v ...interface{}) { slog.Debug(str, v...) } -func (k *kisDefaultSlog) SetDebugMode(enable bool) { +func (k *kisDefaultSlog) SetDebugMode() { k.mu.Lock() defer k.mu.Unlock() - - if enable { - k.level = slog.LevelDebug - } + k.level = slog.LevelDebug } func initDefaultSlog(kisLog *kisDefaultSlog) { diff --git a/log/kis_log.go b/log/kis_log.go index 09fb761..6e8b693 100644 --- a/log/kis_log.go +++ b/log/kis_log.go @@ -18,7 +18,7 @@ type KisLogger interface { DebugF(str string, v ...interface{}) // SetDebugMode 设置Debug模式 - SetDebugMode(enable bool) + SetDebugMode() } // kisLog 默认的KisLog 对象, 提供默认的日志打印方式, 均是打印在标准输出上。 From d7d1383a94dc0e7cf27042cfe531e50092be6b09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=8D=A3=E6=98=8C?= Date: Sun, 7 Apr 2024 22:39:21 +0800 Subject: [PATCH 6/7] fix: rename log interface --- config/kis_func_config.go | 6 +++--- flow/kis_flow.go | 8 ++++---- flow/kis_flow_data.go | 8 ++++---- function/kis_function_c.go | 4 ++-- function/kis_function_e.go | 4 ++-- function/kis_function_l.go | 4 ++-- function/kis_function_s.go | 4 ++-- function/kis_function_v.go | 4 ++-- kis/pool.go | 14 +++++++------- log/kis_default_log.go | 12 ++++++------ log/kis_log.go | 24 ++++++++++++------------ metrics/kis_metrics.go | 2 +- test/faas/faas_demo2.go | 4 ++-- test/kis_config_test.go | 8 ++++---- test/kis_flow_commit_batch_test.go | 2 +- test/kis_log_test.go | 12 ++++++------ 16 files changed, 60 insertions(+), 60 deletions(-) diff --git a/config/kis_func_config.go b/config/kis_func_config.go index 53dbde9..765977e 100644 --- a/config/kis_func_config.go +++ b/config/kis_func_config.go @@ -46,7 +46,7 @@ func NewFuncConfig( Name: "unNamedSource", } source = &defaultSource - log.Logger().InfoF("funcName NewConfig source is nil, use default unNamed Source.", "funcName", funcName) + log.Logger().Info("funcName NewConfig source is nil, use default unNamed Source.", "funcName", funcName) } config.Source = *source @@ -56,10 +56,10 @@ func NewFuncConfig( // FunctionS 和 L 需要必传KisConnector参数,原因是S和L需要通过Connector进行建立流式关系 if mode == common.S || mode == common.L { if option == nil { - log.Logger().ErrorF("Funcion S/L need option->Cid\n") + log.Logger().Error("Funcion S/L need option->Cid\n") return nil } else if option.CName == "" { - log.Logger().ErrorF("Funcion S/L need option->Cid\n") + log.Logger().Error("Funcion S/L need option->Cid\n") return nil } } diff --git a/flow/kis_flow.go b/flow/kis_flow.go index 7e408f9..b84f3c6 100644 --- a/flow/kis_flow.go +++ b/flow/kis_flow.go @@ -97,8 +97,8 @@ func (flow *KisFlow) Fork(ctx context.Context) kis.Flow { } } - log.Logger().DebugFX(ctx, "=====>Flow Fork, ", "oldFlow.funcParams", flow.funcParams) - log.Logger().DebugFX(ctx, "=====>Flow Fork, ", "newFlow.funcParams", newFlow.GetFuncParamsAllFuncs()) + log.Logger().DebugX(ctx, "=====>Flow Fork, ", "oldFlow.funcParams", flow.funcParams) + log.Logger().DebugX(ctx, "=====>Flow Fork, ", "newFlow.funcParams", newFlow.GetFuncParamsAllFuncs()) return newFlow } @@ -257,7 +257,7 @@ func (flow *KisFlow) Run(ctx context.Context) error { // 得到当前Function要处理与的源数据 if inputData, err := flow.getCurData(); err != nil { - log.Logger().ErrorFX(ctx, "flow.Run(): getCurData", "err", err.Error()) + log.Logger().ErrorX(ctx, "flow.Run(): getCurData", "err", err.Error()) return err } else { flow.inPut = inputData @@ -346,7 +346,7 @@ func (flow *KisFlow) GetFuncConfigByName(funcName string) *config.KisFuncConfig if f, ok := flow.Funcs[funcName]; ok { return f.GetConfig() } else { - log.Logger().ErrorF("GetFuncConfigByName(): Function not found", "FunctionName", funcName) + log.Logger().Error("GetFuncConfigByName(): Function not found", "FunctionName", funcName) return nil } } diff --git a/flow/kis_flow_data.go b/flow/kis_flow_data.go index 361966e..588fede 100644 --- a/flow/kis_flow_data.go +++ b/flow/kis_flow_data.go @@ -73,7 +73,7 @@ func (flow *KisFlow) commitSrcData(ctx context.Context) error { metrics.Metrics.FlowDataTotal.WithLabelValues(flow.Name).Add(float64(dataCnt)) } - log.Logger().DebugFX(ctx, "====> After CommitSrcData", "flow_name", flow.Name, "flow_id", flow.Id, "All Level Data", flow.data) + log.Logger().DebugX(ctx, "====> After CommitSrcData", "flow_name", flow.Name, "flow_id", flow.Id, "All Level Data", flow.data) return nil } @@ -106,7 +106,7 @@ func (flow *KisFlow) commitReuseData(ctx context.Context) error { // 清空缓冲Buf (如果是ReuseData选项,那么提交的全部数据,都将不会携带到下一层) flow.buffer = flow.buffer[0:0] - log.Logger().DebugFX(ctx, " ====> After commitReuseData", "flow_name", flow.Name, "flow_id", flow.Id, "All Level Data", flow.data) + log.Logger().DebugX(ctx, " ====> After commitReuseData", "flow_name", flow.Name, "flow_id", flow.Id, "All Level Data", flow.data) return nil } @@ -122,7 +122,7 @@ func (flow *KisFlow) commitVoidData(ctx context.Context) error { // 将本层计算的缓冲数据提交到本层结果数据中 flow.data[flow.ThisFunctionId] = batch - log.Logger().DebugFX(ctx, " ====> After commitVoidData", "flow_name", flow.Name, "flow_id", flow.Id, "All Level Data", flow.data) + log.Logger().DebugX(ctx, " ====> After commitVoidData", "flow_name", flow.Name, "flow_id", flow.Id, "All Level Data", flow.data) return nil } @@ -150,7 +150,7 @@ func (flow *KisFlow) commitCurData(ctx context.Context) error { // 清空缓冲Buf flow.buffer = flow.buffer[0:0] - log.Logger().DebugFX(ctx, " ====> After commitCurData", "flow_name", flow.Name, "flow_id", flow.Id, "All Level Data", flow.data) + log.Logger().DebugX(ctx, " ====> After commitCurData", "flow_name", flow.Name, "flow_id", flow.Id, "All Level Data", flow.data) return nil } diff --git a/function/kis_function_c.go b/function/kis_function_c.go index 3c3baab..39e1367 100644 --- a/function/kis_function_c.go +++ b/function/kis_function_c.go @@ -20,11 +20,11 @@ func NewKisFunctionC() kis.Function { } func (f *KisFunctionC) Call(ctx context.Context, flow kis.Flow) error { - log.Logger().DebugF("KisFunctionC", "flow", flow) + log.Logger().Debug("KisFunctionC", "flow", flow) // 通过KisPool 路由到具体的执行计算Function中 if err := kis.Pool().CallFunction(ctx, f.Config.FName, flow); err != nil { - log.Logger().ErrorFX(ctx, "Function Called Error", "err", err) + log.Logger().ErrorX(ctx, "Function Called Error", "err", err) return err } diff --git a/function/kis_function_e.go b/function/kis_function_e.go index 2f8aace..ff260e8 100644 --- a/function/kis_function_e.go +++ b/function/kis_function_e.go @@ -20,11 +20,11 @@ func NewKisFunctionE() kis.Function { } func (f *KisFunctionE) Call(ctx context.Context, flow kis.Flow) error { - log.Logger().DebugF("KisFunctionE", "flow", flow) + log.Logger().Debug("KisFunctionE", "flow", flow) // 通过KisPool 路由到具体的执行计算Function中 if err := kis.Pool().CallFunction(ctx, f.Config.FName, flow); err != nil { - log.Logger().ErrorFX(ctx, "Function Called Error", "err", err) + log.Logger().ErrorX(ctx, "Function Called Error", "err", err) return err } diff --git a/function/kis_function_l.go b/function/kis_function_l.go index ec6edb5..2e31f2f 100644 --- a/function/kis_function_l.go +++ b/function/kis_function_l.go @@ -20,11 +20,11 @@ func NewKisFunctionL() kis.Function { } func (f *KisFunctionL) Call(ctx context.Context, flow kis.Flow) error { - log.Logger().DebugF("KisFunctionL", "flow", flow) + log.Logger().Debug("KisFunctionL", "flow", flow) // 通过KisPool 路由到具体的执行计算Function中 if err := kis.Pool().CallFunction(ctx, f.Config.FName, flow); err != nil { - log.Logger().ErrorFX(ctx, "Function Called Error", "err", err) + log.Logger().ErrorX(ctx, "Function Called Error", "err", err) return err } diff --git a/function/kis_function_s.go b/function/kis_function_s.go index 5afdc30..7f65f82 100644 --- a/function/kis_function_s.go +++ b/function/kis_function_s.go @@ -20,11 +20,11 @@ func NewKisFunctionS() kis.Function { } func (f *KisFunctionS) Call(ctx context.Context, flow kis.Flow) error { - log.Logger().DebugF("KisFunctionS", "flow", flow) + log.Logger().Debug("KisFunctionS", "flow", flow) // 通过KisPool 路由到具体的执行计算Function中 if err := kis.Pool().CallFunction(ctx, f.Config.FName, flow); err != nil { - log.Logger().ErrorFX(ctx, "Function Called Error", "err", err) + log.Logger().ErrorX(ctx, "Function Called Error", "err", err) return err } diff --git a/function/kis_function_v.go b/function/kis_function_v.go index 3e10b02..0892573 100644 --- a/function/kis_function_v.go +++ b/function/kis_function_v.go @@ -20,11 +20,11 @@ func NewKisFunctionV() kis.Function { } func (f *KisFunctionV) Call(ctx context.Context, flow kis.Flow) error { - log.Logger().DebugF("KisFunctionV", "flow", flow) + log.Logger().Debug("KisFunctionV", "flow", flow) // 通过KisPool 路由到具体的执行计算Function中 if err := kis.Pool().CallFunction(ctx, f.Config.FName, flow); err != nil { - log.Logger().ErrorFX(ctx, "Function Called Error", "err", err) + log.Logger().ErrorX(ctx, "Function Called Error", "err", err) return err } diff --git a/kis/pool.go b/kis/pool.go index bd42f95..42886f8 100644 --- a/kis/pool.go +++ b/kis/pool.go @@ -61,7 +61,7 @@ func (pool *kisPool) AddFlow(name string, flow Flow) { panic(errString) } - log.Logger().InfoF("Add FlowRouter", "FlowName", name) + log.Logger().Info("Add FlowRouter", "FlowName", name) } func (pool *kisPool) GetFlow(name string) Flow { @@ -95,7 +95,7 @@ func (pool *kisPool) FaaS(fnName string, f FaaS) { panic(errString) } - log.Logger().InfoF("Add KisPool", "FuncName", fnName) + log.Logger().Info("Add KisPool", "FuncName", fnName) } // CallFunction 调度 Function @@ -127,7 +127,7 @@ func (pool *kisPool) CallFunction(ctx context.Context, fnName string, flow Flow) // 将flow.Input()中的原始数据,反序列化为argType类型的数据 value, err := funcDesc.Serialize.UnMarshal(flow.Input(), argType) if err != nil { - log.Logger().ErrorFX(ctx, "funcDesc.Serialize.DecodeParam", "err", err) + log.Logger().ErrorX(ctx, "funcDesc.Serialize.DecodeParam", "err", err) } else { params = append(params, value) continue @@ -153,7 +153,7 @@ func (pool *kisPool) CallFunction(ctx context.Context, fnName string, flow Flow) } - log.Logger().ErrorFX(ctx, "FuncName: Can not find in KisPool, Not Added.", "FuncName", fnName) + log.Logger().ErrorX(ctx, "FuncName: Can not find in KisPool, Not Added.", "FuncName", fnName) return errors.New("FuncName: " + fnName + " Can not find in NsPool, Not Added.") } @@ -170,7 +170,7 @@ func (pool *kisPool) CaaSInit(cname string, c ConnInit) { panic(errString) } - log.Logger().InfoF("Add KisPool CaaSInit", "CName", cname) + log.Logger().Info("Add KisPool CaaSInit", "CName", cname) } // CallConnInit 调度 ConnInit @@ -208,7 +208,7 @@ func (pool *kisPool) CaaS(cname string, fname string, mode common.KisMode, c Caa panic(errString) } - log.Logger().InfoF("Add KisPool CaaS", "CName", cname, "FName", fname, "Mode", mode) + log.Logger().Info("Add KisPool CaaS", "CName", cname, "FName", fname, "Mode", mode) } // CallConnector 调度 Connector @@ -223,7 +223,7 @@ func (pool *kisPool) CallConnector(ctx context.Context, flow Flow, conn Connecto return callback(ctx, conn, fn, flow, args) } - log.Logger().ErrorFX(ctx, "Can not find in KisPool, Not Added.", "CName", conn.GetName(), " FName", fnConf.FName, "mode", mode) + log.Logger().ErrorX(ctx, "Can not find in KisPool, Not Added.", "CName", conn.GetName(), " FName", fnConf.FName, "mode", mode) return nil, errors.New(fmt.Sprintf("CName:%s FName:%s mode:%s Can not find in KisPool, Not Added.", conn.GetName(), fnConf.FName, mode)) } diff --git a/log/kis_default_log.go b/log/kis_default_log.go index a74eeab..208994d 100644 --- a/log/kis_default_log.go +++ b/log/kis_default_log.go @@ -78,27 +78,27 @@ func getKisDefaultSLog(opts ...KisLogOptions) *kisDefaultSlog { return defaultKisSlog } -func (k *kisDefaultSlog) InfoFX(ctx context.Context, str string, v ...interface{}) { +func (k *kisDefaultSlog) InfoX(ctx context.Context, str string, v ...interface{}) { slog.InfoContext(ctx, str, v...) } -func (k *kisDefaultSlog) ErrorFX(ctx context.Context, str string, v ...interface{}) { +func (k *kisDefaultSlog) ErrorX(ctx context.Context, str string, v ...interface{}) { slog.ErrorContext(ctx, str, v...) } -func (k *kisDefaultSlog) DebugFX(ctx context.Context, str string, v ...interface{}) { +func (k *kisDefaultSlog) DebugX(ctx context.Context, str string, v ...interface{}) { slog.DebugContext(ctx, str, v...) } -func (k *kisDefaultSlog) InfoF(str string, v ...interface{}) { +func (k *kisDefaultSlog) Info(str string, v ...interface{}) { slog.Info(str, v...) } -func (k *kisDefaultSlog) ErrorF(str string, v ...interface{}) { +func (k *kisDefaultSlog) Error(str string, v ...interface{}) { slog.Error(str, v...) } -func (k *kisDefaultSlog) DebugF(str string, v ...interface{}) { +func (k *kisDefaultSlog) Debug(str string, v ...interface{}) { slog.Debug(str, v...) } diff --git a/log/kis_log.go b/log/kis_log.go index 6e8b693..52b90c2 100644 --- a/log/kis_log.go +++ b/log/kis_log.go @@ -3,19 +3,19 @@ package log import "context" type KisLogger interface { - // InfoFX 有上下文的Info级别日志接口, format字符串格式 - InfoFX(ctx context.Context, str string, v ...interface{}) - // ErrorFX 有上下文的Error级别日志接口, format字符串格式 - ErrorFX(ctx context.Context, str string, v ...interface{}) - // DebugFX 有上下文的Debug级别日志接口, format字符串格式 - DebugFX(ctx context.Context, str string, v ...interface{}) + // InfoX 有上下文的Info级别日志接口, format字符串格式 + InfoX(ctx context.Context, str string, v ...interface{}) + // ErrorX 有上下文的Error级别日志接口, format字符串格式 + ErrorX(ctx context.Context, str string, v ...interface{}) + // DebugX 有上下文的Debug级别日志接口, format字符串格式 + DebugX(ctx context.Context, str string, v ...interface{}) - // InfoF 无上下文的Info级别日志接口, format字符串格式 - InfoF(str string, v ...interface{}) - // ErrorF 无上下文的Error级别日志接口, format字符串格式 - ErrorF(str string, v ...interface{}) - // DebugF 无上下文的Debug级别日志接口, format字符串格式 - DebugF(str string, v ...interface{}) + // Info 无上下文的Info级别日志接口, format字符串格式 + Info(str string, v ...interface{}) + // Error 无上下文的Error级别日志接口, format字符串格式 + Error(str string, v ...interface{}) + // Debug 无上下文的Debug级别日志接口, format字符串格式 + Debug(str string, v ...interface{}) // SetDebugMode 设置Debug模式 SetDebugMode() diff --git a/metrics/kis_metrics.go b/metrics/kis_metrics.go index 248f909..4116937 100644 --- a/metrics/kis_metrics.go +++ b/metrics/kis_metrics.go @@ -36,7 +36,7 @@ func RunMetricsService(serverAddr string) error { // 启动HttpServer err := http.ListenAndServe(serverAddr, nil) // 多个进程不可监听同一个端口 if err != nil { - log.Logger().ErrorF("RunMetricsService", "err", err) + log.Logger().Error("RunMetricsService", "err", err) } return err diff --git a/test/faas/faas_demo2.go b/test/faas/faas_demo2.go index 1929eb5..a498eed 100644 --- a/test/faas/faas_demo2.go +++ b/test/faas/faas_demo2.go @@ -19,12 +19,12 @@ func FuncDemo2Handler(ctx context.Context, flow kis.Flow) error { conn, err := flow.GetConnector() if err != nil { - log.Logger().ErrorFX(ctx, "FuncDemo2Handler(): GetConnector", "err", err.Error()) + log.Logger().ErrorX(ctx, "FuncDemo2Handler(): GetConnector", "err", err.Error()) return err } if _, err := conn.Call(ctx, flow, row); err != nil { - log.Logger().ErrorFX(ctx, "FuncDemo2Handler(): Call", "err", err.Error()) + log.Logger().ErrorX(ctx, "FuncDemo2Handler(): Call", "err", err.Error()) return err } diff --git a/test/kis_config_test.go b/test/kis_config_test.go index 550b3c4..67194d1 100644 --- a/test/kis_config_test.go +++ b/test/kis_config_test.go @@ -26,7 +26,7 @@ func TestNewFuncConfig(t *testing.T) { myFunc1 := config.NewFuncConfig("funcName1", common.S, &source, &option) - log.Logger().InfoF("funcName1", myFunc1) + log.Logger().Info("funcName1", myFunc1) } func TestNewFlowConfig(t *testing.T) { @@ -50,7 +50,7 @@ func TestNewFlowConfig(t *testing.T) { myFlow1.AppendFunctionConfig(flowFuncParams1) myFlow1.AppendFunctionConfig(flowFuncParams2) - log.Logger().InfoF("myFlow1", myFlow1) + log.Logger().Info("myFlow1", myFlow1) } func TestNewConnConfig(t *testing.T) { @@ -81,8 +81,8 @@ func TestNewConnConfig(t *testing.T) { myConnector1 := config.NewConnConfig("connectorName1", "0.0.0.0:9987,0.0.0.0:9997", common.REDIS, "key", connParams) if err := myConnector1.WithFunc(myFunc1); err != nil { - log.Logger().ErrorF("WithFunc", "err", err.Error()) + log.Logger().Error("WithFunc", "err", err.Error()) } - log.Logger().InfoF("myConnector1", myConnector1) + log.Logger().Info("myConnector1", myConnector1) } diff --git a/test/kis_flow_commit_batch_test.go b/test/kis_flow_commit_batch_test.go index 2ef4792..e22a0ac 100644 --- a/test/kis_flow_commit_batch_test.go +++ b/test/kis_flow_commit_batch_test.go @@ -27,7 +27,7 @@ func TestForkFlowCommitBatch(t *testing.T) { // 3. 提交原始数据 if err := flow1.CommitRowBatch(stringRows); err != nil { - log.Logger().ErrorF("CommitRowBatch Error", "err", err) + log.Logger().Error("CommitRowBatch Error", "err", err) panic(err) } diff --git a/test/kis_log_test.go b/test/kis_log_test.go index 37736cb..d9bd30e 100644 --- a/test/kis_log_test.go +++ b/test/kis_log_test.go @@ -9,11 +9,11 @@ import ( func TestKisLogger(t *testing.T) { ctx := context.Background() - log.Logger().InfoFX(ctx, "TestKisLogger InfoFX") - log.Logger().ErrorFX(ctx, "TestKisLogger ErrorFX") - log.Logger().DebugFX(ctx, "TestKisLogger DebugFX") + log.Logger().InfoX(ctx, "TestKisLogger InfoX") + log.Logger().ErrorX(ctx, "TestKisLogger ErrorX") + log.Logger().DebugX(ctx, "TestKisLogger DebugX") - log.Logger().InfoF("TestKisLogger InfoF") - log.Logger().ErrorF("TestKisLogger ErrorF") - log.Logger().DebugF("TestKisLogger DebugF") + log.Logger().Info("TestKisLogger Info") + log.Logger().Error("TestKisLogger Error") + log.Logger().Debug("TestKisLogger Debug") } From 5577322a3d0e30b30f529b35e7b47dd8cd9ee4be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=8D=A3=E6=98=8C?= Date: Mon, 8 Apr 2024 19:03:20 +0800 Subject: [PATCH 7/7] =?UTF-8?q?fix:=20=E5=85=BC=E5=AE=B9=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=20logFormat=20=E6=A0=BC=E5=BC=8F=E5=8C=96=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- log/kis_default_log.go | 26 ++++++++++++++++++++++++++ log/kis_log.go | 14 ++++++++++++++ test/kis_log_test.go | 11 +++++++++++ 3 files changed, 51 insertions(+) diff --git a/log/kis_default_log.go b/log/kis_default_log.go index 208994d..914e6ea 100644 --- a/log/kis_default_log.go +++ b/log/kis_default_log.go @@ -2,6 +2,7 @@ package log import ( "context" + "fmt" "io" "log/slog" "os" @@ -78,6 +79,31 @@ func getKisDefaultSLog(opts ...KisLogOptions) *kisDefaultSlog { return defaultKisSlog } +func (k *kisDefaultSlog) InfoFX(ctx context.Context, str string, v ...interface{}) { + slog.InfoContext(ctx, fmt.Sprintf(str, v...)) +} + +func (k *kisDefaultSlog) ErrorFX(ctx context.Context, str string, v ...interface{}) { + slog.ErrorContext(ctx, fmt.Sprintf(str, v...)) +} + +func (k *kisDefaultSlog) DebugFX(ctx context.Context, str string, v ...interface{}) { + slog.DebugContext(ctx, fmt.Sprintf(str, v...)) +} + +// InfoF 使用格式化格式(xxxF或xxxFX)要使用 fmt.Sprintf() 函数进行格式化包装 +func (k *kisDefaultSlog) InfoF(str string, v ...interface{}) { + slog.Info(fmt.Sprintf(str, v...)) +} + +func (k *kisDefaultSlog) ErrorF(str string, v ...interface{}) { + slog.Error(fmt.Sprintf(str, v...)) +} + +func (k *kisDefaultSlog) DebugF(str string, v ...interface{}) { + slog.Debug(fmt.Sprintf(str, v...)) +} + func (k *kisDefaultSlog) InfoX(ctx context.Context, str string, v ...interface{}) { slog.InfoContext(ctx, str, v...) } diff --git a/log/kis_log.go b/log/kis_log.go index 52b90c2..381c04e 100644 --- a/log/kis_log.go +++ b/log/kis_log.go @@ -3,6 +3,20 @@ package log import "context" type KisLogger interface { + // InfoFX 有上下文的Info级别日志接口, format字符串格式 + InfoFX(ctx context.Context, str string, v ...interface{}) + // ErrorFX 有上下文的Error级别日志接口, format字符串格式 + ErrorFX(ctx context.Context, str string, v ...interface{}) + // DebugFX 有上下文的Debug级别日志接口, format字符串格式 + DebugFX(ctx context.Context, str string, v ...interface{}) + + // InfoF 无上下文的Info级别日志接口, format字符串格式 + InfoF(str string, v ...interface{}) + // ErrorF 无上下文的Error级别日志接口, format字符串格式 + ErrorF(str string, v ...interface{}) + // DebugF 无上下文的Debug级别日志接口, format字符串格式 + DebugF(str string, v ...interface{}) + // InfoX 有上下文的Info级别日志接口, format字符串格式 InfoX(ctx context.Context, str string, v ...interface{}) // ErrorX 有上下文的Error级别日志接口, format字符串格式 diff --git a/test/kis_log_test.go b/test/kis_log_test.go index d9bd30e..d05a023 100644 --- a/test/kis_log_test.go +++ b/test/kis_log_test.go @@ -9,6 +9,17 @@ import ( func TestKisLogger(t *testing.T) { ctx := context.Background() + log.Logger().DebugF("TestKisLogger Format DebugF name = %s, age = %d", "kisFlow", 23) + log.Logger().ErrorF("TestKisLogger Format ErrorF name = %s, age = %d", "kisFlow", 12) + log.Logger().InfoF("TestKisLogger Format InfoF name = %s, stu =%+v", "kisFlow", + struct { + name string + age int + }{ + name: "kisName", + age: 12, + }) + log.Logger().InfoX(ctx, "TestKisLogger InfoX") log.Logger().ErrorX(ctx, "TestKisLogger ErrorX") log.Logger().DebugX(ctx, "TestKisLogger DebugX")