feat: global set log level

This commit is contained in:
debugtalk
2021-10-18 09:52:57 +08:00
parent 0efcb0a8d1
commit b345b455eb
10 changed files with 52 additions and 11 deletions

View File

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

View File

@@ -12,11 +12,12 @@ import (
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/httprunner/hrp" "github.com/httprunner/hrp"
) )
var log = hrp.GetLogger()
const ( const (
suffixJSON = ".json" suffixJSON = ".json"
suffixYAML = ".yaml" suffixYAML = ".yaml"

View File

@@ -18,6 +18,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),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
hrp.SetLogLevel(logLevel)
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})

View File

@@ -4,6 +4,7 @@ import (
"github.com/rs/zerolog/log" "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"
) )
@@ -14,6 +15,7 @@ var har2caseCmd = &cobra.Command{
Long: `Convert HAR to json/yaml testcase files`, Long: `Convert HAR to json/yaml testcase files`,
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
hrp.SetLogLevel(logLevel)
var outputFiles []string var outputFiles []string
for _, arg := range args { for _, arg := range args {
var outputPath string var outputPath string

View File

@@ -21,9 +21,17 @@ Copyright 2021 debugtalk`,
Version: hrp.VERSION, Version: hrp.VERSION,
} }
var (
logLevel string
logJSON bool
)
// Execute adds all child commands to the root command and sets flags appropriately. // Execute adds all child commands to the root command and sets flags appropriately.
// 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() { func Execute() {
RootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "INFO", "set log level")
RootCmd.PersistentFlags().BoolVar(&logJSON, "log-json", false, "set log to json format")
if err := RootCmd.Execute(); err != nil { if err := RootCmd.Execute(); err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@@ -16,6 +16,7 @@ var runCmd = &cobra.Command{
$ hrp run examples/ # run testcases in specified folder`, $ hrp run examples/ # run testcases in specified folder`,
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
hrp.SetLogLevel(logLevel)
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})

38
log.go Normal file
View File

@@ -0,0 +1,38 @@
package hrp
import (
"os"
"strings"
"github.com/rs/zerolog"
zlog "github.com/rs/zerolog/log"
)
var log = zlog.Logger
func init() {
log = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
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 GetLogger() zerolog.Logger {
return log
}

View File

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

View File

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

View File

@@ -2,19 +2,12 @@ package hrp
import ( import (
"net/http" "net/http"
"os"
"testing" "testing"
"github.com/imroc/req" "github.com/imroc/req"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
) )
func init() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
// run API test with default configs // run API test with default configs
func Run(t *testing.T, testcases ...ITestCase) error { func Run(t *testing.T, testcases ...ITestCase) error {
return NewRunner().WithTestingT(t).SetDebug(true).Run(testcases...) return NewRunner().WithTestingT(t).SetDebug(true).Run(testcases...)