add kisLogger

This commit is contained in:
aceld 2024-01-02 15:38:51 +08:00
parent 047273e3e8
commit c2b2c1ab02
9 changed files with 94 additions and 19 deletions

View File

@ -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
}
}

View File

@ -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执行方法

View File

@ -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执行方法

View File

@ -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执行方法

View File

@ -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执行方法

View File

@ -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执行方法

43
log/kis_default_log.go Normal file
View File

@ -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{})
}
}

32
log/kis_log.go Normal file
View File

@ -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
}

View File

@ -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)
}