diff --git a/config/kis_func_config.go b/config/kis_func_config.go index 6beab51..8a5f36a 100644 --- a/config/kis_func_config.go +++ b/config/kis_func_config.go @@ -1,8 +1,8 @@ package config import ( - "fmt" "kis-flow/common" + "kis-flow/log" ) // FParam 在当前Flow中Function定制固定配置参数类型 @@ -42,7 +42,7 @@ func NewFuncConfig( config.Fname = funcName if source == nil { - fmt.Printf("funcName NewConfig Error, source is nil, funcName = %s\n", funcId) + log.Logger().ErrorF("funcName NewConfig Error, source is nil, funcName = %s\n", funcId) return nil } config.Source = *source @@ -52,10 +52,10 @@ func NewFuncConfig( //FunctionS 和 L 需要必传KisConnector参数,原因是S和L需要通过Connector进行建立流式关系 if mode == common.S || mode == common.L { if option == nil { - fmt.Printf("Funcion S/L need option->Cid\n") + log.Logger().ErrorF("Funcion S/L need option->Cid\n") return nil } else if option.Cid == "" { - fmt.Printf("Funcion S/L need option->Cid\n") + log.Logger().ErrorF("Funcion S/L need option->Cid\n") return nil } } diff --git a/function/kis_function_c.go b/function/kis_function_c.go index 0c92e12..a52daf6 100644 --- a/function/kis_function_c.go +++ b/function/kis_function_c.go @@ -2,8 +2,8 @@ package function import ( "context" - "fmt" "kis-flow/kis" + "kis-flow/log" ) type KisFunctionC struct { @@ -11,7 +11,7 @@ type KisFunctionC struct { } func (f *KisFunctionC) Call(ctx context.Context, flow kis.Flow) error { - fmt.Printf("KisFunctionC, flow = %+v\n", flow) + log.Logger().InfoF("KisFunctionC, flow = %+v\n", flow) // TODO 调用具体的Function执行方法 diff --git a/function/kis_function_e.go b/function/kis_function_e.go index c26bc75..7a87817 100644 --- a/function/kis_function_e.go +++ b/function/kis_function_e.go @@ -2,8 +2,8 @@ package function import ( "context" - "fmt" "kis-flow/kis" + "kis-flow/log" ) type KisFunctionE struct { @@ -11,7 +11,7 @@ type KisFunctionE struct { } func (f *KisFunctionE) Call(ctx context.Context, flow kis.Flow) error { - fmt.Printf("KisFunctionE, flow = %+v\n", flow) + log.Logger().InfoF("KisFunctionE, flow = %+v\n", flow) // TODO 调用具体的Function执行方法 diff --git a/function/kis_function_l.go b/function/kis_function_l.go index 77c5126..a0ef4ec 100644 --- a/function/kis_function_l.go +++ b/function/kis_function_l.go @@ -2,8 +2,8 @@ package function import ( "context" - "fmt" "kis-flow/kis" + "kis-flow/log" ) type KisFunctionL struct { @@ -11,7 +11,7 @@ type KisFunctionL struct { } func (f *KisFunctionL) Call(ctx context.Context, flow kis.Flow) error { - fmt.Printf("KisFunctionL, flow = %+v\n", flow) + log.Logger().InfoF("KisFunctionL, flow = %+v\n", flow) // TODO 调用具体的Function执行方法 diff --git a/function/kis_function_s.go b/function/kis_function_s.go index 3e06695..36a1a4d 100644 --- a/function/kis_function_s.go +++ b/function/kis_function_s.go @@ -2,8 +2,8 @@ package function import ( "context" - "fmt" "kis-flow/kis" + "kis-flow/log" ) type KisFunctionS struct { @@ -11,7 +11,7 @@ type KisFunctionS struct { } func (f *KisFunctionS) Call(ctx context.Context, flow kis.Flow) error { - fmt.Printf("KisFunctionS, flow = %+v\n", flow) + log.Logger().InfoF("KisFunctionS, flow = %+v\n", flow) // TODO 调用具体的Function执行方法 diff --git a/function/kis_function_v.go b/function/kis_function_v.go index fd02514..07f9de6 100644 --- a/function/kis_function_v.go +++ b/function/kis_function_v.go @@ -2,8 +2,8 @@ package function import ( "context" - "fmt" "kis-flow/kis" + "kis-flow/log" ) type KisFunctionV struct { @@ -11,7 +11,7 @@ type KisFunctionV struct { } func (f *KisFunctionV) Call(ctx context.Context, flow kis.Flow) error { - fmt.Printf("KisFunctionV, flow = %+v\n", flow) + log.Logger().InfoF("KisFunctionV, flow = %+v\n", flow) // TODO 调用具体的Function执行方法 diff --git a/log/kis_default_log.go b/log/kis_default_log.go new file mode 100644 index 0000000..9a04288 --- /dev/null +++ b/log/kis_default_log.go @@ -0,0 +1,43 @@ +package log + +import ( + "context" + "fmt" +) + +// kisDefaultLog 默认提供的日志对象 +type kisDefaultLog struct{} + +func (log *kisDefaultLog) InfoF(str string, v ...interface{}) { + fmt.Printf(str, v...) +} + +func (log *kisDefaultLog) ErrorF(str string, v ...interface{}) { + fmt.Printf(str, v...) +} + +func (log *kisDefaultLog) DebugF(str string, v ...interface{}) { + fmt.Printf(str, v...) +} + +func (log *kisDefaultLog) InfoFX(ctx context.Context, str string, v ...interface{}) { + fmt.Println(ctx) + fmt.Printf(str, v...) +} + +func (log *kisDefaultLog) ErrorFX(ctx context.Context, str string, v ...interface{}) { + fmt.Println(ctx) + fmt.Printf(str, v...) +} + +func (log *kisDefaultLog) DebugFX(ctx context.Context, str string, v ...interface{}) { + fmt.Println(ctx) + fmt.Printf(str, v...) +} + +func init() { + // 如果没有设置Logger, 则启动时使用默认的kisDefaultLog对象 + if Logger() == nil { + SetLogger(&kisDefaultLog{}) + } +} diff --git a/log/kis_log.go b/log/kis_log.go new file mode 100644 index 0000000..477e381 --- /dev/null +++ b/log/kis_log.go @@ -0,0 +1,32 @@ +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{}) +} + +// kisLog 默认的KisLog 对象, 提供默认的日志打印方式, 均是打印在标准输出上。 +var kisLog KisLogger + +// SetLogger 设置KisLog对象, 可以是用户自定义的Logger对象 +func SetLogger(newlog KisLogger) { + kisLog = newlog +} + +// Logger 获取到kisLog对象 +func Logger() KisLogger { + return kisLog +} diff --git a/test/kis_config_test.go b/test/kis_config_test.go index 1c52495..b1ffab9 100644 --- a/test/kis_config_test.go +++ b/test/kis_config_test.go @@ -1,9 +1,9 @@ package test import ( - "fmt" "kis-flow/common" "kis-flow/config" + "kis-flow/log" "testing" ) @@ -28,7 +28,7 @@ func TestNewFlowConfig(t *testing.T) { myFlow1.AppendFunctionConfig(flowFuncParams1) myFlow1.AppendFunctionConfig(flowFuncParams2) - fmt.Printf("myFlow1: %+v\n", myFlow1) + log.Logger().InfoF("myFlow1: %+v\n", myFlow1) } func TestNewFuncConfig(t *testing.T) { @@ -50,7 +50,7 @@ func TestNewFuncConfig(t *testing.T) { myFunc1 := config.NewFuncConfig("funcId", "funcName", "Save", &source, &option) - fmt.Printf("myFunc1: %+v\n", myFunc1) + log.Logger().InfoF("myFunc1: %+v\n", myFunc1) } func TestNewConnConfig(t *testing.T) { @@ -81,8 +81,8 @@ func TestNewConnConfig(t *testing.T) { myConnector1 := config.NewConnConfig("connectorId", "connectorName", "0.0.0.0:9987,0.0.0.0:9997", common.REDIS, "key", connParams) if err := myConnector1.WithFunc(myFunc1); err != nil { - fmt.Printf("WithFunc err: %s\n", err.Error()) + log.Logger().ErrorF("WithFunc err: %s\n", err.Error()) } - fmt.Printf("myConnector1: %+v\n", myConnector1) + log.Logger().InfoF("myConnector1: %+v\n", myConnector1) }