From b77f93533994224c495ff85245068664bf0a4b82 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sun, 17 Oct 2021 14:51:44 +0800 Subject: [PATCH] change: replace logs --- convert.go | 2 +- har2case/core.go | 6 +++++- parser.go | 9 +++++---- response.go | 11 ++++++++--- runner.go | 23 ++++++++++++++++------- 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/convert.go b/convert.go index c8e12217..88961ba7 100644 --- a/convert.go +++ b/convert.go @@ -70,7 +70,7 @@ func loadFromJSON(path string) (*TCase, error) { log.Errorf("convert absolute path error: %v, path: %v", err, path) return nil, err } - log.Infof("load testcase from json path: %s", path) + log.WithField("path", path).Info("load json testcase") file, err := ioutil.ReadFile(path) if err != nil { diff --git a/har2case/core.go b/har2case/core.go index 22f4e805..7926b9e0 100644 --- a/har2case/core.go +++ b/har2case/core.go @@ -120,7 +120,11 @@ func (h *HAR) prepareTestSteps() ([]*hrp.TStep, error) { } func (h *HAR) prepareTestStep(entry *Entry) (*hrp.TStep, error) { - log.Infof("[prepareTestStep] %v %v", entry.Request.Method, entry.Request.URL) + log.WithFields(log.Fields{ + "method": entry.Request.Method, + "url": entry.Request.URL, + }).Info("convert teststep") + tStep := &TStep{ TStep: hrp.TStep{ Request: &hrp.TRequest{}, diff --git a/parser.go b/parser.go index a13f56f3..5975b24f 100644 --- a/parser.go +++ b/parser.go @@ -291,10 +291,7 @@ func callFunc(funcName string, arguments ...interface{}) (interface{}, error) { argumentsValue[index] = argumentValue.Convert(expectArgumentType) } - log.Infof("[callFunction] func: %v, input arguments: %v", funcName, arguments) resultValues := funcValue.Call(argumentsValue) - log.Infof("[callFunction] output values: %v", resultValues) - if len(resultValues) > 1 { // function should return at most one value err := fmt.Errorf("function %s should return at most one value", funcName) @@ -310,7 +307,11 @@ func callFunc(funcName string, arguments ...interface{}) (interface{}, error) { // return one value // convert reflect.Value to interface{} result := resultValues[0].Interface() - log.Infof("[callFunction] output result: %+v(%T)", result, result) + log.WithFields(log.Fields{ + "funcName": funcName, + "arguments": arguments, + "output": result, + }).Info("call function") return result, nil } diff --git a/response.go b/response.go index 4e08f216..4eabe547 100644 --- a/response.go +++ b/response.go @@ -77,8 +77,8 @@ func (v *ResponseObject) Extract(extractors map[string]string) map[string]interf extractMapping := make(map[string]interface{}) for key, value := range extractors { extractedValue := v.searchJmespath(value) - log.Infof("[extract] %s => %v", value, extractedValue) - log.Infof("[setVariable] %s = %v", key, extractedValue) + log.WithField("value", extractedValue).Infof("extract value from %s", value) + log.WithField("value", extractedValue).Infof("set variable %s", key) extractMapping[key] = extractedValue } @@ -112,7 +112,12 @@ func (v *ResponseObject) Validate(validators []TValidator, variablesMapping map[ // do assertion result := assertFunc(v.t, expectValue, checkValue) - log.Infof("[assert] %s <%s> %v => %v", checkItem, assertMethod, expectValue, result) + log.WithFields(log.Fields{ + "assertMethod": assertMethod, + "expectValue": expectValue, + "checkValue": checkValue, + "result": result, + }).Infof("validate %s", checkItem) if !result { v.t.Fail() } diff --git a/runner.go b/runner.go index f0a91e65..803d3f5e 100644 --- a/runner.go +++ b/runner.go @@ -9,6 +9,11 @@ import ( log "github.com/sirupsen/logrus" ) +func init() { + log.SetLevel(log.InfoLevel) + log.SetFormatter(&log.TextFormatter{}) +} + // run API test with default configs func Run(t *testing.T, testcases ...ITestCase) error { return NewRunner().WithTestingT(t).SetDebug(true).Run(testcases...) @@ -29,19 +34,19 @@ type Runner struct { } func (r *Runner) WithTestingT(t *testing.T) *Runner { - log.Infof("[init] WithTestingT: %v", t) + log.Info("[init] WithTestingT") r.t = t return r } func (r *Runner) SetDebug(debug bool) *Runner { - log.Infof("[init] SetDebug: %v", debug) + log.WithField("debug", debug).Info("[init] SetDebug") r.debug = debug return r } func (r *Runner) SetProxyUrl(proxyUrl string) *Runner { - log.Infof("[init] SetProxyUrl: %s", proxyUrl) + log.WithField("proxyUrl", proxyUrl).Info("[init] SetProxyUrl") r.client.SetProxyUrl(proxyUrl) return r } @@ -67,7 +72,7 @@ func (r *Runner) runCase(testcase *TestCase) error { return err } - log.Infof("Start to run testcase: %v", config.Name) + log.WithField("testcase", config.Name).Info("run testcase start") extractedVariables := make(map[string]interface{}) @@ -96,11 +101,12 @@ func (r *Runner) runCase(testcase *TestCase) error { } } + log.WithField("testcase", config.Name).Info("run testcase end") return nil } func (r *Runner) runStep(step IStep, config *TConfig) (stepData *StepData, err error) { - log.Infof("run step [%v] begin >>>>>>", step.Name()) + log.WithField("step", step.Name()).Info("run step start") if tc, ok := step.(*testcaseWithOptionalArgs); ok { // run referenced testcase log.Infof("run referenced testcase: %v", tc.step.Name) @@ -117,8 +123,11 @@ func (r *Runner) runStep(step IStep, config *TConfig) (stepData *StepData, err e return } } - log.Infof("run step [%v] end, success: %v, responseLength: %v <<<<<<\n", - step.Name(), stepData.Success, stepData.ResponseLength) + log.WithFields(log.Fields{ + "step": step.Name(), + "success": stepData.Success, + "exportVars": stepData.ExportVars, + }).Info("run step end") return }