Skip to content

Commit

Permalink
refactor: logger flags
Browse files Browse the repository at this point in the history
  • Loading branch information
iseki0 committed Oct 23, 2024
1 parent ee834bb commit b60cd8f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
9 changes: 6 additions & 3 deletions cmd/murphy/internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ var (
EnableNetworkLogging bool
NoLogFile bool
LogFileOverride string
LogLevel logger.Level
LogLevel = LogLevelFlag{
Level: logger.LevelSilent,
Valid: true,
}
)

func GetToken(ctx context.Context) (string, error) {
Expand Down Expand Up @@ -102,8 +105,8 @@ func InitLogger0(ctx context.Context, mergeToStdout bool) (context.Context, erro
} else {
stderr = zapcore.Lock(os.Stderr)
}
if LogLevel > logger.LevelSilent {
consoleCore = zapcore.NewCore(logger.ZapConsoleEncoder, stderr, LogLevel.ZapLevel())
if LogLevel.Level > logger.LevelSilent {
consoleCore = zapcore.NewCore(logger.ZapConsoleEncoder, stderr, LogLevel.Level.ZapLevel())
}

loggerCore := zapcore.NewTee(consoleCore, jsonCore)
Expand Down
33 changes: 33 additions & 0 deletions cmd/murphy/internal/common/log_flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package common

import (
"github.com/murphysecurity/murphysec/logger"
"github.com/spf13/pflag"
"strings"
)

type LogLevelFlag struct {
Level logger.Level
Valid bool
}

var _ pflag.Value

func (l *LogLevelFlag) String() string {
if !l.Valid {
return "unset"
}
return l.Level.String()
}
func (l *LogLevelFlag) Set(s string) error {
var ll logger.Level
if e := ll.Of(strings.ToLower(s)); e != nil {
return e
}
l.Level = ll
l.Valid = true
return nil
}
func (l *LogLevelFlag) Type() string {
return "logLevelFlag"
}
3 changes: 1 addition & 2 deletions cmd/murphy/internal/internalcmd/scanner_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/murphysecurity/murphysec/infra/logctx"
"github.com/murphysecurity/murphysec/infra/ui"
"github.com/murphysecurity/murphysec/inspector"
logger2 "github.com/murphysecurity/murphysec/logger"
"github.com/murphysecurity/murphysec/model"
"github.com/murphysecurity/murphysec/scanerr"
"github.com/murphysecurity/murphysec/utils"
Expand All @@ -37,7 +36,7 @@ func scannerScanRun(cmd *cobra.Command, args []string) {
)
ctx = scanerr.WithCtx(ctx)
env.ScannerScan = true
common.LogLevel = logger2.LevelDebug
must.Must(common.LogLevel.Set("debug"))
ctx, e = common.InitLogger0(ctx, true)
if e != nil {
fmt.Fprintf(os.Stderr, "init logger failed: %v\n", e)
Expand Down
7 changes: 1 addition & 6 deletions cmd/murphy/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
)

var versionFlag bool
var consoleLogLevelOverride string

func preRun(cmd *cobra.Command, args []string) error {
if versionFlag {
Expand All @@ -29,10 +28,6 @@ func preRun(cmd *cobra.Command, args []string) error {

logger.LogFileCleanup()

if e := common.LogLevel.Of(consoleLogLevelOverride); e != nil {
return e
}

return nil
}

Expand All @@ -52,7 +47,7 @@ func rootCmd() *cobra.Command {
// Logging
c.PersistentFlags().BoolVar(&common.NoLogFile, "no-log-file", false, "do not write log file")
c.PersistentFlags().StringVar(&common.LogFileOverride, "write-log-to", "", "specify log file path")
c.PersistentFlags().StringVar(&consoleLogLevelOverride, "log-level", "silent", "specify log level, must be silent|error|warn|info|debug")
c.PersistentFlags().Var(&common.LogLevel, "log-level", "specify log level, must be silent|error|warn|info|debug")
c.PersistentFlags().BoolVar(&common.EnableNetworkLogging, "network-log", false, "print network data")
_ = c.PersistentFlags().MarkHidden("network-log")

Expand Down

0 comments on commit b60cd8f

Please sign in to comment.