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