refactor: logger

This commit is contained in:
debugtalk
2021-12-22 18:53:36 +08:00
parent a845b58ff5
commit 2c0772ce30
15 changed files with 48 additions and 62 deletions

View File

@@ -3,6 +3,8 @@ package hrp
import ( import (
"time" "time"
"github.com/rs/zerolog/log"
"github.com/httprunner/hrp/internal/boomer" "github.com/httprunner/hrp/internal/boomer"
"github.com/httprunner/hrp/internal/ga" "github.com/httprunner/hrp/internal/ga"
) )

View File

@@ -7,6 +7,7 @@ import (
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )

View File

@@ -22,4 +22,4 @@ Copyright 2021 debugtalk
* [hrp har2case](hrp_har2case.md) - Convert HAR to json/yaml testcase files * [hrp har2case](hrp_har2case.md) - Convert HAR to json/yaml testcase files
* [hrp run](hrp_run.md) - run API test * [hrp run](hrp_run.md) - run API test
###### Auto generated by spf13/cobra on 21-Dec-2021 ###### Auto generated by spf13/cobra on 22-Dec-2021

View File

@@ -30,7 +30,6 @@ hrp boom [flags]
--mem-profile-duration duration Memory profile duration. (default 30s) --mem-profile-duration duration Memory profile duration. (default 30s)
--prometheus-gateway string Prometheus Pushgateway url. --prometheus-gateway string Prometheus Pushgateway url.
--request-increase-rate string Request increase rate, disabled by default. (default "-1") --request-increase-rate string Request increase rate, disabled by default. (default "-1")
--run-tasks string Run tasks without connecting to the master, multiply tasks is separated by comma. Usually, it's for debug purpose.
--spawn-count int The number of users to spawn for load testing (default 1) --spawn-count int The number of users to spawn for load testing (default 1)
--spawn-rate float The rate for spawning users (default 1) --spawn-rate float The rate for spawning users (default 1)
``` ```
@@ -39,4 +38,4 @@ hrp boom [flags]
* [hrp](hrp.md) - One-stop solution for HTTP(S) testing. * [hrp](hrp.md) - One-stop solution for HTTP(S) testing.
###### Auto generated by spf13/cobra on 21-Dec-2021 ###### Auto generated by spf13/cobra on 22-Dec-2021

View File

@@ -23,4 +23,4 @@ hrp har2case harPath... [flags]
* [hrp](hrp.md) - One-stop solution for HTTP(S) testing. * [hrp](hrp.md) - One-stop solution for HTTP(S) testing.
###### Auto generated by spf13/cobra on 21-Dec-2021 ###### Auto generated by spf13/cobra on 22-Dec-2021

View File

@@ -31,4 +31,4 @@ hrp run path... [flags]
* [hrp](hrp.md) - One-stop solution for HTTP(S) testing. * [hrp](hrp.md) - One-stop solution for HTTP(S) testing.
###### Auto generated by spf13/cobra on 21-Dec-2021 ###### Auto generated by spf13/cobra on 22-Dec-2021

View File

@@ -12,21 +12,18 @@ import (
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog" "github.com/rs/zerolog/log"
"github.com/httprunner/hrp" "github.com/httprunner/hrp"
"github.com/httprunner/hrp/internal/ga" "github.com/httprunner/hrp/internal/ga"
) )
var log zerolog.Logger
const ( const (
suffixJSON = ".json" suffixJSON = ".json"
suffixYAML = ".yaml" suffixYAML = ".yaml"
) )
func NewHAR(path string) *har { func NewHAR(path string) *har {
log = hrp.GetLogger()
return &har{ return &har{
path: path, path: path,
} }

View File

@@ -19,7 +19,7 @@ var boomCmd = &cobra.Command{
$ hrp boom examples/ # run testcases in specified folder`, $ hrp boom examples/ # run testcases in specified folder`,
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
hrp.SetLogger("WARN", logJSON) // disable info logs for load testing setLogLevel("WARN") // disable info logs for load testing
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var paths []hrp.ITestCase var paths []hrp.ITestCase

View File

@@ -1,9 +1,9 @@
package cmd package cmd
import ( import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/httprunner/hrp"
"github.com/httprunner/hrp/har2case" "github.com/httprunner/hrp/har2case"
) )
@@ -13,6 +13,9 @@ var har2caseCmd = &cobra.Command{
Short: "Convert HAR to json/yaml testcase files", Short: "Convert HAR to json/yaml testcase files",
Long: `Convert HAR to json/yaml testcase files`, Long: `Convert HAR to json/yaml testcase files`,
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
setLogLevel(logLevel)
},
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
var outputFiles []string var outputFiles []string
for _, arg := range args { for _, arg := range args {
@@ -37,7 +40,6 @@ var har2caseCmd = &cobra.Command{
} }
outputFiles = append(outputFiles, outputPath) outputFiles = append(outputFiles, outputPath)
} }
log := hrp.GetLogger()
log.Info().Strs("output", outputFiles).Msg("convert testcase success") log.Info().Strs("output", outputFiles).Msg("convert testcase success")
return nil return nil
}, },

View File

@@ -3,10 +3,12 @@ package cmd
import ( import (
"fmt" "fmt"
"os" "os"
"strings"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/httprunner/hrp"
"github.com/httprunner/hrp/internal/version" "github.com/httprunner/hrp/internal/version"
) )
@@ -20,7 +22,10 @@ License: Apache-2.0
Github: https://github.com/httprunner/hrp Github: https://github.com/httprunner/hrp
Copyright 2021 debugtalk`, Copyright 2021 debugtalk`,
PersistentPreRun: func(cmd *cobra.Command, args []string) { PersistentPreRun: func(cmd *cobra.Command, args []string) {
hrp.SetLogger(logLevel, logJSON) if !logJSON {
log.Logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).With().Timestamp().Logger()
log.Info().Msg("Set log to color console other than JSON format.")
}
}, },
Version: version.VERSION, Version: version.VERSION,
} }
@@ -41,3 +46,22 @@ func Execute() {
os.Exit(1) os.Exit(1)
} }
} }
func setLogLevel(level string) {
level = strings.ToUpper(level)
log.Info().Msgf("Set log level to %s", level)
switch level {
case "DEBUG":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "INFO":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "WARN":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "ERROR":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
case "FATAL":
zerolog.SetGlobalLevel(zerolog.FatalLevel)
case "PANIC":
zerolog.SetGlobalLevel(zerolog.PanicLevel)
}
}

View File

@@ -15,12 +15,17 @@ var runCmd = &cobra.Command{
$ hrp run demo.yaml # run specified yaml testcase file $ hrp run demo.yaml # run specified yaml testcase file
$ hrp run examples/ # run testcases in specified folder`, $ hrp run examples/ # run testcases in specified folder`,
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
setLogLevel(logLevel)
},
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
var paths []hrp.ITestCase var paths []hrp.ITestCase
for _, arg := range args { for _, arg := range args {
paths = append(paths, &hrp.TestCasePath{Path: arg}) paths = append(paths, &hrp.TestCasePath{Path: arg})
} }
runner := hrp.NewRunner(nil).SetDebug(!silentFlag).SetFailfast(!continueOnFailure) runner := hrp.NewRunner(nil).
SetDebug(!silentFlag).
SetFailfast(!continueOnFailure)
if proxyUrl != "" { if proxyUrl != "" {
runner.SetProxyUrl(proxyUrl) runner.SetProxyUrl(proxyUrl)
} }

47
log.go
View File

@@ -1,47 +0,0 @@
package hrp
import (
"os"
"strings"
"github.com/rs/zerolog"
zlog "github.com/rs/zerolog/log"
)
var log = zlog.Logger
// SetLogger configures the log level and format.
func SetLogger(level string, logJSON bool) {
if !logJSON {
setLogPretty()
}
setLogLevel(level)
}
func setLogLevel(level string) {
level = strings.ToUpper(level)
log.Info().Msgf("Set log level to %s", level)
switch level {
case "DEBUG":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "INFO":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "WARN":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "ERROR":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
case "FATAL":
zerolog.SetGlobalLevel(zerolog.FatalLevel)
case "PANIC":
zerolog.SetGlobalLevel(zerolog.PanicLevel)
}
}
func setLogPretty() {
log = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
log.Info().Msg("Set log to color console other than JSON format.")
}
func GetLogger() zerolog.Logger {
return log
}

View File

@@ -9,6 +9,7 @@ import (
"strings" "strings"
"github.com/maja42/goval" "github.com/maja42/goval"
"github.com/rs/zerolog/log"
"github.com/httprunner/hrp/internal/builtin" "github.com/httprunner/hrp/internal/builtin"
) )

View File

@@ -8,6 +8,7 @@ import (
"testing" "testing"
"github.com/jmespath/go-jmespath" "github.com/jmespath/go-jmespath"
"github.com/rs/zerolog/log"
"github.com/httprunner/hrp/internal/builtin" "github.com/httprunner/hrp/internal/builtin"
) )

View File

@@ -16,6 +16,7 @@ import (
"github.com/jinzhu/copier" "github.com/jinzhu/copier"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/httprunner/hrp/internal/ga" "github.com/httprunner/hrp/internal/ga"
) )