Skip to content

Commit

Permalink
Merge pull request #9 from buty4649/improve-debug-logger
Browse files Browse the repository at this point in the history
Add logger to iproute2 and update slog usage
  • Loading branch information
buty4649 authored Mar 20, 2024
2 parents 6f0bbc2 + 31876a3 commit 0e21a3c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,20 @@ var rootCmd = &cobra.Command{
if flags.Quiet {
logLevel = slog.LevelError
}
slog.SetDefault(slog.New(slogor.NewHandler(os.Stdout, slogor.Options{
logger := slog.New(slogor.NewHandler(os.Stdout, slogor.Options{
TimeFormat: "",
Level: logLevel,
ShowSource: false,
})))
}))
slog.SetDefault(logger)

cfg, err = config.LoadConfig(flags.ConfigPath)
if err != nil {
return err
}

ip = iproute2.New(flags.IpCmdPath)
iproute2.SetLogger(logger)
return nil
},
}
Expand Down
10 changes: 9 additions & 1 deletion iproute2/iproute2.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type Error struct {
Message string
}

var logger *slog.Logger

func New(path string) *Iproute2 {
return &Iproute2{
path: path,
Expand All @@ -51,6 +53,10 @@ func New(path string) *Iproute2 {
}
}

func SetLogger(l *slog.Logger) {
logger = l
}

func (i *Iproute2) AddLink(name string, linkType string, options ...string) error {
args := []string{"link", "add", "name", name, "type", linkType}
args = append(args, options...)
Expand Down Expand Up @@ -189,7 +195,9 @@ func (i *Iproute2) executeWithStdout(args ...string) (string, error) {
}
cmdArgs = append(cmdArgs, args...)

slog.Debug(fmt.Sprintf("exec: %s %s", i.path, strings.Join(cmdArgs, " ")))
if logger != nil {
logger.Debug("exec command", "path", i.path, "args", strings.Join(cmdArgs, " "))
}

cmd := exec.Command(i.path, cmdArgs...)
var stdoutBuf, stderrBuf bytes.Buffer
Expand Down

0 comments on commit 0e21a3c

Please sign in to comment.