change: update logs

This commit is contained in:
lilong.129
2023-08-06 15:17:07 +08:00
parent 6e8c0005c0
commit 9a7848a142
7 changed files with 65 additions and 72 deletions
-4
View File
@@ -7,7 +7,6 @@ import (
"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"
) )
@@ -15,9 +14,6 @@ var rootCmd = &cobra.Command{
Use: "wcl", Use: "wcl",
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) {
hrp.InitLogger("INFO", false)
},
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
var device uixt.Device var device uixt.Device
var bundleID string var bundleID string
+53 -2
View File
@@ -1,9 +1,16 @@
package cmd package cmd
import ( import (
"os"
"runtime"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/rs/zerolog/pkgerrors"
"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"
@@ -31,7 +38,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) {
hrp.InitLogger(logLevel, logJSON) initLogger(logLevel, logJSON)
}, },
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
@@ -57,3 +64,47 @@ func Execute() int {
err := rootCmd.Execute() err := rootCmd.Execute()
return code.GetErrorCode(err) return code.GetErrorCode(err)
} }
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 = zerolog.New(
zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.RFC3339,
NoColor: noColor,
},
).With().Timestamp().Logger()
log.Info().Msg("log with colorized console")
} else {
// default logger
log.Info().Msg("log with json output")
log.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)
}
}
-56
View File
@@ -1,56 +0,0 @@
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)
}
}
+1 -1
View File
@@ -73,7 +73,7 @@ func (ad *adbDriver) WindowSize() (size Size, err error) {
var resolution string var resolution string
sizeList := strings.Split(output, "\n") sizeList := strings.Split(output, "\n")
log.Info().Msgf("window size: %v", sizeList) log.Trace().Msgf("window size: %v", sizeList)
for _, size := range sizeList { for _, size := range sizeList {
if strings.Contains(size, matchedSizeType) { if strings.Contains(size, matchedSizeType) {
resolution = strings.Split(size, ": ")[1] resolution = strings.Split(size, ": ")[1]
+3 -3
View File
@@ -8,11 +8,11 @@ import (
"image" "image"
"image/color" "image/color"
"io/ioutil" "io/ioutil"
"log"
"math" "math"
"os" "os"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog/log"
"gocv.io/x/gocv" "gocv.io/x/gocv"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin" "github.com/httprunner/httprunner/v4/hrp/internal/builtin"
@@ -409,14 +409,14 @@ func getMatchingLocation(matImage gocv.Mat, matTpl gocv.Mat, threshold float32,
val, loc = getValLoc(minVal, maxVal, minLoc, maxLoc, matchMode) val, loc = getValLoc(minVal, maxVal, minLoc, maxLoc, matchMode)
if debug == DmEachMatch { if debug == DmEachMatch {
log.Println(fmt.Sprintf(dmOutputMsg, val, threshold)) log.Debug().Msg(fmt.Sprintf(dmOutputMsg, val, threshold))
} }
if val >= threshold { if val >= threshold {
return loc, nil return loc, nil
} else { } else {
if debug == DmNotMatch { if debug == DmNotMatch {
log.Println(fmt.Sprintf(dmOutputMsg, val, threshold)) log.Debug().Msg(fmt.Sprintf(dmOutputMsg, val, threshold))
} }
return image.Point{}, errors.New("no such target search image") return image.Point{}, errors.New("no such target search image")
} }
+2
View File
@@ -429,6 +429,7 @@ func (r *CaseRunner) parseConfig() error {
} }
iosDeviceConfig.UDID = udid.(string) iosDeviceConfig.UDID = udid.(string)
} }
device, err := uixt.NewIOSDevice(uixt.GetIOSDeviceOptions(iosDeviceConfig)...) device, err := uixt.NewIOSDevice(uixt.GetIOSDeviceOptions(iosDeviceConfig)...)
if err != nil { if err != nil {
return errors.Wrap(err, "init iOS device failed") return errors.Wrap(err, "init iOS device failed")
@@ -447,6 +448,7 @@ func (r *CaseRunner) parseConfig() error {
} }
androidDeviceConfig.SerialNumber = sn.(string) androidDeviceConfig.SerialNumber = sn.(string)
} }
device, err := uixt.NewAndroidDevice(uixt.GetAndroidDeviceOptions(androidDeviceConfig)...) device, err := uixt.NewAndroidDevice(uixt.GetAndroidDeviceOptions(androidDeviceConfig)...)
if err != nil { if err != nil {
return errors.Wrap(err, "init Android device failed") return errors.Wrap(err, "init Android device failed")