Skip to content

Commit

Permalink
Ensure we log into logrus on command error
Browse files Browse the repository at this point in the history
`urfave/cli` now takes upon itself to log the error returned by the
command action directly. This means that by default the `--log` option
was ignored upon error.

This commit ensure that `urfave/cli.ErrWriter` will use logrus

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
  • Loading branch information
mlaventure committed Sep 30, 2016
1 parent 3597b7b commit de4dd42
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io"
"os"
"strings"

Expand Down Expand Up @@ -45,6 +46,8 @@ your host. Providing the bundle directory using "-b" is optional. The default
value for "bundle" is the current directory.`
)

var originalCliErrWriter io.Writer

func main() {
app := cli.NewApp()
app.Name = "runc"
Expand Down Expand Up @@ -129,7 +132,22 @@ func main() {
}
return nil
}
// If the command returns an error, cli takes upon itself to print
// the error on cli.ErrWriter and exit.
// Use our own writer here to ensure the log gets sent to the right location.
originalCliErrWriter = cli.ErrWriter
cli.ErrWriter = &FatalWriter{}
if err := app.Run(os.Args); err != nil {
fatal(err)
}
}

type FatalWriter struct {
}

func (FatalWriter) Write(p []byte) (n int, err error) {
msg := string(p)
logrus.Error(msg)
fmt.Fprint(originalCliErrWriter, msg)
return len(p), nil
}

0 comments on commit de4dd42

Please sign in to comment.