Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logger to iproute2 and update slog usage #9

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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