mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-07 00:39:34 +08:00
refactor: init logger
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/net/context"
|
||||
@@ -26,9 +27,10 @@ var boomCmd = &cobra.Command{
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
boomer.SetUlimit(10240) // ulimit -n 10240
|
||||
if !strings.EqualFold(logLevel, "DEBUG") {
|
||||
logLevel = "WARN" // disable info logs for load testing
|
||||
// disable info logs for load testing
|
||||
log.Info().Msg("Set global log level to WARN for load testing")
|
||||
zerolog.SetGlobalLevel(zerolog.WarnLevel)
|
||||
}
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
startTime := time.Now()
|
||||
|
||||
@@ -17,9 +17,6 @@ var buildCmd = &cobra.Command{
|
||||
Example: ` $ hrp build plugin/debugtalk.go
|
||||
$ hrp build plugin/debugtalk.py`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
startTime := time.Now()
|
||||
defer func() {
|
||||
|
||||
@@ -19,9 +19,6 @@ var convertCmd = &cobra.Command{
|
||||
Short: "convert multiple source format to HttpRunner JSON/YAML/gotest/pytest cases",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
SilenceUsage: false,
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
caseConverter := convert.NewConverter(outputDir, profilePath)
|
||||
|
||||
|
||||
@@ -15,12 +15,9 @@ import (
|
||||
)
|
||||
|
||||
var pytestCmd = &cobra.Command{
|
||||
Use: "pytest $path ...",
|
||||
Short: "run API test with pytest",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
Use: "pytest $path ...",
|
||||
Short: "run API test with pytest",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
DisableFlagParsing: true, // allow to pass any args to pytest
|
||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
startTime := time.Now()
|
||||
|
||||
@@ -1,14 +1,9 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp"
|
||||
"github.com/httprunner/httprunner/v4/hrp/cmd/adb"
|
||||
"github.com/httprunner/httprunner/v4/hrp/cmd/ios"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/code"
|
||||
@@ -36,14 +31,7 @@ Website: https://httprunner.com
|
||||
Github: https://github.com/httprunner/httprunner
|
||||
Copyright 2017 debugtalk`,
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
noColor := false
|
||||
if runtime.GOOS == "windows" {
|
||||
noColor = true
|
||||
}
|
||||
if !logJSON {
|
||||
log.Logger = zerolog.New(zerolog.ConsoleWriter{NoColor: noColor, Out: os.Stderr}).With().Timestamp().Logger()
|
||||
log.Info().Msg("Set log to color console")
|
||||
}
|
||||
hrp.InitLogger(logLevel, logJSON)
|
||||
},
|
||||
Version: version.VERSION,
|
||||
TraverseChildren: true, // parses flags on all parents before executing child command
|
||||
@@ -60,7 +48,7 @@ var (
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() int {
|
||||
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "INFO", "set log level")
|
||||
rootCmd.PersistentFlags().BoolVar(&logJSON, "log-json", false, "set log to json format")
|
||||
rootCmd.PersistentFlags().BoolVar(&logJSON, "log-json", false, "set log to json format (default colorized console)")
|
||||
rootCmd.PersistentFlags().StringVar(&venv, "venv", "", "specify python3 venv path")
|
||||
|
||||
ios.Init(rootCmd)
|
||||
@@ -69,22 +57,3 @@ func Execute() int {
|
||||
err := rootCmd.Execute()
|
||||
return code.GetErrorCode(err)
|
||||
}
|
||||
|
||||
func setLogLevel(level string) {
|
||||
level = strings.ToUpper(level)
|
||||
log.Info().Str("level", level).Msg("Set log 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,9 +15,6 @@ 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 {
|
||||
|
||||
@@ -14,9 +14,6 @@ var scaffoldCmd = &cobra.Command{
|
||||
Aliases: []string{"scaffold"},
|
||||
Short: "create a scaffold project",
|
||||
Args: cobra.ExactValidArgs(1),
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if !ignorePlugin && !genPythonPlugin && !genGoPlugin {
|
||||
return errors.New("please specify function plugin type")
|
||||
|
||||
@@ -14,9 +14,6 @@ var wikiCmd = &cobra.Command{
|
||||
Use: "wiki",
|
||||
Aliases: []string{"info", "docs", "doc"},
|
||||
Short: "visit https://httprunner.com",
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
startTime := time.Now()
|
||||
defer func() {
|
||||
|
||||
Reference in New Issue
Block a user