mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-11 18:11:21 +08:00
refactor: logger
This commit is contained in:
@@ -3,6 +3,8 @@ package hrp
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/hrp/internal/boomer"
|
||||
"github.com/httprunner/hrp/internal/ga"
|
||||
)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
|
||||
@@ -22,4 +22,4 @@ Copyright 2021 debugtalk
|
||||
* [hrp har2case](hrp_har2case.md) - Convert HAR to json/yaml testcase files
|
||||
* [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
|
||||
|
||||
@@ -30,7 +30,6 @@ hrp boom [flags]
|
||||
--mem-profile-duration duration Memory profile duration. (default 30s)
|
||||
--prometheus-gateway string Prometheus Pushgateway url.
|
||||
--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-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.
|
||||
|
||||
###### Auto generated by spf13/cobra on 21-Dec-2021
|
||||
###### Auto generated by spf13/cobra on 22-Dec-2021
|
||||
|
||||
@@ -23,4 +23,4 @@ hrp har2case harPath... [flags]
|
||||
|
||||
* [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
|
||||
|
||||
@@ -31,4 +31,4 @@ hrp run path... [flags]
|
||||
|
||||
* [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
|
||||
|
||||
@@ -12,21 +12,18 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/hrp"
|
||||
"github.com/httprunner/hrp/internal/ga"
|
||||
)
|
||||
|
||||
var log zerolog.Logger
|
||||
|
||||
const (
|
||||
suffixJSON = ".json"
|
||||
suffixYAML = ".yaml"
|
||||
)
|
||||
|
||||
func NewHAR(path string) *har {
|
||||
log = hrp.GetLogger()
|
||||
return &har{
|
||||
path: path,
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ var boomCmd = &cobra.Command{
|
||||
$ hrp boom examples/ # run testcases in specified folder`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
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) {
|
||||
var paths []hrp.ITestCase
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/hrp"
|
||||
"github.com/httprunner/hrp/har2case"
|
||||
)
|
||||
|
||||
@@ -13,6 +13,9 @@ var har2caseCmd = &cobra.Command{
|
||||
Short: "Convert HAR to json/yaml testcase files",
|
||||
Long: `Convert HAR to json/yaml testcase files`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
var outputFiles []string
|
||||
for _, arg := range args {
|
||||
@@ -37,7 +40,6 @@ var har2caseCmd = &cobra.Command{
|
||||
}
|
||||
outputFiles = append(outputFiles, outputPath)
|
||||
}
|
||||
log := hrp.GetLogger()
|
||||
log.Info().Strs("output", outputFiles).Msg("convert testcase success")
|
||||
return nil
|
||||
},
|
||||
|
||||
@@ -3,10 +3,12 @@ package cmd
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/hrp"
|
||||
"github.com/httprunner/hrp/internal/version"
|
||||
)
|
||||
|
||||
@@ -20,7 +22,10 @@ License: Apache-2.0
|
||||
Github: https://github.com/httprunner/hrp
|
||||
Copyright 2021 debugtalk`,
|
||||
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,
|
||||
}
|
||||
@@ -41,3 +46,22 @@ func Execute() {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,12 +15,17 @@ var runCmd = &cobra.Command{
|
||||
$ hrp run demo.yaml # run specified yaml testcase file
|
||||
$ hrp run examples/ # run testcases in specified folder`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
var paths []hrp.ITestCase
|
||||
for _, arg := range args {
|
||||
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 != "" {
|
||||
runner.SetProxyUrl(proxyUrl)
|
||||
}
|
||||
|
||||
47
log.go
47
log.go
@@ -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
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/maja42/goval"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/hrp/internal/builtin"
|
||||
)
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/jmespath/go-jmespath"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/hrp/internal/builtin"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user