Skip to content

Commit c9fef2d

Browse files
committed
log&udf
1 parent 4f095b0 commit c9fef2d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+550
-109
lines changed

api/engine_handler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"github.com/gin-gonic/gin"
55
"github.com/skyhackvip/risk_engine/core"
66
"github.com/skyhackvip/risk_engine/internal/dto"
7+
"github.com/skyhackvip/risk_engine/internal/log"
78
"github.com/skyhackvip/risk_engine/service"
8-
"log"
99
"net/http"
1010
)
1111

@@ -30,14 +30,14 @@ func (handler *EngineHandler) Run(c *gin.Context) {
3030
})
3131
return
3232
}
33-
log.Printf("======[trace]request start req_id (%s)======\n", request.ReqId)
33+
log.Infof("======[trace] request start req_id %s======", request.ReqId)
3434
svr := service.NewEngineService(handler.kernel)
3535
result, err := svr.Run(c, &request)
3636
if err != nil {
3737
code = 501
3838
errs = err.Error()
3939
}
40-
log.Printf("======[trace]request end req_id (%s)======\n", request.ReqId)
40+
log.Infof("======[trace] request end req_id %s======", request.ReqId)
4141
c.JSON(http.StatusOK, gin.H{
4242
"code": code,
4343
"result": result,

api/router.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"github.com/gin-gonic/gin"
66
"github.com/skyhackvip/risk_engine/core"
77
"github.com/skyhackvip/risk_engine/global"
8-
"log"
8+
"github.com/skyhackvip/risk_engine/internal/log"
99
)
1010

1111
func Init() { //conf
@@ -20,5 +20,5 @@ func Init() { //conf
2020

2121
router.Run(fmt.Sprintf(":%d", global.ServerConf.Port)) //conf
2222

23-
log.Printf("[HTTP] Listening on: %s\n", global.ServerConf.Port)
23+
log.Infof("[HTTP] Listening on %d", global.ServerConf.Port)
2424
}

cmd/risk_engine/config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Server:
44
ReadTimeout: 10
55
WriteTimeout: 10
66
App:
7-
LogPath: log/app.log
7+
LogMethod: console
8+
LogPath: ./log/risk_engine.log
89
DslLoadMethod: file
910
DslLoadPath: demo/ #注意实际放置目录

cmd/risk_engine/engine.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/skyhackvip/risk_engine/api"
77
"github.com/skyhackvip/risk_engine/configs"
88
"github.com/skyhackvip/risk_engine/global"
9-
"log"
9+
"github.com/skyhackvip/risk_engine/internal/log"
1010
"os"
1111
"os/signal"
1212
"syscall"
@@ -23,22 +23,22 @@ func main() {
2323
global.ServerConf = &conf.Server
2424
global.AppConf = &conf.App
2525

26+
log.InitLogger(global.AppConf.LogMethod, global.AppConf.LogPath)
27+
2628
api.Init()
2729

2830
//graceful restart
2931
quit := make(chan os.Signal)
3032
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)
3133
<-quit
32-
log.Println("shutdown risk engine...")
33-
//cancel
34+
log.Info("shutdown risk engine...")
35+
3436
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
3537
defer cancel()
36-
/*if err := srv.Shutdown(ctx); err != nil {
37-
log.Fatal("server shutdown error:", err)
38-
}*/
38+
3939
select {
4040
case <-ctx.Done():
41-
log.Println("timeout of 5 seconds")
41+
log.Warn("timeout of 5 seconds")
4242
}
43-
log.Println("server exiting")
43+
log.Info("server exiting")
4444
}

configs/config.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ type ServerConf struct {
1919
}
2020

2121
type AppConf struct {
22-
LogFile string `yaml:"LogFile"`
23-
DslLoadMethod string `yaml:"DslLoadMethod"`
22+
LogMethod string `yaml:"LogMethod"` //console,file
23+
LogPath string `yaml:"LogPath"`
24+
DslLoadMethod string `yaml:"DslLoadMethod"` //file,db
2425
DslLoadPath string `yaml:"DslLoadPath"`
2526
}
2627

@@ -43,3 +44,9 @@ func LoadConfig(path string) (*Conf, error) {
4344
}
4445
return conf, nil
4546
}
47+
48+
const (
49+
CONSOLE = "console"
50+
FILE = "file"
51+
DB = "db"
52+
)

core/abtest.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package core
22

33
import (
44
"github.com/skyhackvip/risk_engine/internal/errcode"
5-
"log"
5+
"github.com/skyhackvip/risk_engine/internal/log"
66
"math/rand"
77
"time"
88
)
@@ -34,7 +34,7 @@ func (node AbtestNode) AfterParse(ctx *PipelineContext, result *NodeResult) erro
3434

3535
func (abtest AbtestNode) Parse(ctx *PipelineContext) (*NodeResult, error) {
3636
info := abtest.GetInfo()
37-
log.Printf("======[trace]abtest(%s, %s) start======\n", info.Label, abtest.GetName())
37+
log.Infof("======[trace] abtest %s start======", info.Label, abtest.GetName())
3838
nodeResult := &NodeResult{Id: info.Id, Name: info.Name, Kind: abtest.GetType(), Tag: info.Tag, Label: info.Label, IsBlock: false}
3939

4040
rand.Seed(time.Now().UnixNano())
@@ -44,7 +44,7 @@ func (abtest AbtestNode) Parse(ctx *PipelineContext) (*NodeResult, error) {
4444
for _, branch := range abtest.Branchs {
4545
counter += branch.Percent
4646
if counter > winNum {
47-
log.Printf(" abtest %v : %v, %v, output:%v \n", abtest.GetName(), branch.Name, winNum, branch.Decision.Output)
47+
log.Infof("abtest name %s, branch %s, randomNum %v, output %v", abtest.GetName(), branch.Name, winNum, branch.Decision.Output)
4848
nodeResult.NextNodeName = branch.Decision.Output.Value.(string)
4949
nodeResult.NextNodeType = GetNodeType(branch.Decision.Output.Kind)
5050
matchBranch = true
@@ -53,7 +53,7 @@ func (abtest AbtestNode) Parse(ctx *PipelineContext) (*NodeResult, error) {
5353
}
5454
nodeResult.Value = winNum
5555

56-
log.Printf("======[trace]abtest(%s, %s) end======\n", info.Label, abtest.GetName())
56+
log.Infof("======[trace] abtest %s end======", info.Label, abtest.GetName())
5757
if matchBranch {
5858
return nodeResult, nil
5959
}

core/common.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"errors"
55
"fmt"
66
"github.com/skyhackvip/risk_engine/internal/errcode"
7+
"github.com/skyhackvip/risk_engine/internal/log"
78
"github.com/skyhackvip/risk_engine/internal/operator"
8-
"log"
99
)
1010

1111
type NodeInfo struct {
@@ -41,7 +41,7 @@ func (rule *Rule) Parse(ctx *PipelineContext, depends map[string]IFeature) (outp
4141
//rule.Conditions
4242
if len(rule.Conditions) == 0 {
4343
err = errors.New(fmt.Sprintf("rule (%s) condition is empty", rule.Name))
44-
log.Println(err)
44+
log.Error(err)
4545
return
4646
}
4747

@@ -50,13 +50,13 @@ func (rule *Rule) Parse(ctx *PipelineContext, depends map[string]IFeature) (outp
5050
if feature, ok := depends[condition.Feature]; ok {
5151
rs, err := feature.Compare(condition.Operator, condition.Value)
5252
if err != nil {
53-
log.Println(err)
53+
log.Error(err)
5454
return output, nil //value deafult
5555
}
5656
conditionRet[condition.Name] = rs
5757
} else {
5858
//lack of feature whether ignore
59-
log.Printf("error lack of feature: %s\n", condition.Feature)
59+
log.Error("error lack of feature: %s", condition.Feature)
6060
//continue
6161
}
6262
}
@@ -72,7 +72,7 @@ func (rule *Rule) Parse(ctx *PipelineContext, depends map[string]IFeature) (outp
7272
if err != nil {
7373
return
7474
}
75-
log.Printf("rule %s (%s) decision is: %v, output: %v\n", rule.Label, rule.Name, logicRet, rule.Decision.Output)
75+
log.Infof("rule result: %v", rule.Label, rule.Name, logicRet)
7676
output.SetHit(logicRet)
7777

7878
//assign
@@ -139,9 +139,14 @@ func (block Block) parse(depends map[string]IFeature) (interface{}, bool, error)
139139
}
140140
for _, condition := range block.Conditions {
141141
if feature, ok := depends[block.Feature]; ok {
142+
if v, _ := feature.GetValue(); v == nil {
143+
log.Errorf("feature %s empty", feature.GetName())
144+
continue
145+
}
146+
142147
hit, err := feature.Compare(condition.Operator, condition.Value)
143148
if err != nil {
144-
log.Println("parse error", err)
149+
log.Errorf("parse error %s", err)
145150
continue
146151
}
147152
if hit {
@@ -152,7 +157,7 @@ func (block Block) parse(depends map[string]IFeature) (interface{}, bool, error)
152157
}
153158
}
154159
} else {
155-
log.Printf("error lack of feature: %s\n", block.Feature)
160+
log.Errorf("lack of feature: %s", block.Feature)
156161
continue
157162
}
158163
}

core/conditional.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package core
22

33
import (
44
"github.com/skyhackvip/risk_engine/internal/errcode"
5+
"github.com/skyhackvip/risk_engine/internal/log"
56
"github.com/skyhackvip/risk_engine/internal/operator"
6-
"log"
77
)
88

99
type ConditionalNode struct {
@@ -33,7 +33,7 @@ func (node ConditionalNode) AfterParse(ctx *PipelineContext, result *NodeResult)
3333

3434
func (conditional ConditionalNode) Parse(ctx *PipelineContext) (*NodeResult, error) {
3535
info := conditional.GetInfo()
36-
log.Printf("====[trace]conditional (%s, %s) start=====\n", info.Label, conditional.GetName())
36+
log.Infof("====[trace] conditional %s start=====", info.Label, conditional.GetName())
3737
nodeResult := &NodeResult{Id: info.Id, Name: info.Name, Kind: conditional.GetType(), Tag: info.Tag, Label: info.Label, IsBlock: false}
3838

3939
depends := ctx.GetFeatures(info.Depends)
@@ -48,7 +48,7 @@ func (conditional ConditionalNode) Parse(ctx *PipelineContext) (*NodeResult, err
4848
}
4949
conditionRet[condition.Name] = rs
5050
} else { //get feature fail
51-
log.Printf("error lack of feature: %s\n", condition.Feature)
51+
log.Errorf("error lack of feature: %s", condition.Feature)
5252
continue
5353
}
5454
}
@@ -60,15 +60,15 @@ func (conditional ConditionalNode) Parse(ctx *PipelineContext) (*NodeResult, err
6060
continue
6161
}
6262
if logicRs { //if true, choose the branch and break
63-
log.Printf("conditional %v : %v, output:%v \n", conditional.GetName(), branch.Name, branch.Decision.Output)
63+
log.Infof("conditional name %s, branch %s, output %s", conditional.GetName(), branch.Name, branch.Decision.Output)
6464
nodeResult.Value = branch.Name
6565
nodeResult.NextNodeName = branch.Decision.Output.Value.(string)
6666
nodeResult.NextNodeType = GetNodeType(branch.Decision.Output.Kind)
6767
matchBranch = true
6868
break
6969
}
7070
}
71-
log.Printf("====[trace]conditional (%s, %s) end=====\n", info.Label, conditional.GetName())
71+
log.Infof("====[trace] conditional %s end=====", info.Label, conditional.GetName())
7272
if matchBranch {
7373
return nodeResult, nil
7474
}

core/dsl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package core
22

33
import (
4-
"log"
4+
"github.com/skyhackvip/risk_engine/internal/log"
55
)
66

77
type Dsl struct {
@@ -96,7 +96,7 @@ func (dsl *Dsl) ConvertToDecisionFlow() (*DecisionFlow, error) {
9696
newNode.SetElem(scorecardMap[newNode.NodeName])
9797
flow.AddNode(&newNode)
9898
default:
99-
log.Printf("dsl (%s-%s) convert warning: unkown node type (%s)\n", dsl.Key, dsl.Version, newNode.NodeKind)
99+
log.Warnf("dsl %s - %s convert warning: unkown node type %s", dsl.Key, dsl.Version, newNode.NodeKind)
100100
}
101101
}
102102
return flow, nil

core/end.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package core
22

33
import (
4-
// "github.com/skyhackvip/risk_engine/configs"
5-
"log"
4+
"github.com/skyhackvip/risk_engine/internal/log"
65
)
76

87
type EndNode struct {
@@ -36,7 +35,7 @@ func (node EndNode) AfterParse(ctx *PipelineContext, result *NodeResult) error {
3635
}
3736

3837
func (node EndNode) Parse(ctx *PipelineContext) (*NodeResult, error) {
39-
log.Println("======[trace]End======")
38+
log.Info("======[trace] End======")
4039
info := node.GetInfo()
4140
nodeResult := &NodeResult{Id: info.Id, Name: info.Name, Kind: node.GetType(), Tag: info.Tag, Label: info.Label, IsBlock: true}
4241
return nodeResult, nil

0 commit comments

Comments
 (0)