From 294d24fb1a011ab828e383c761a96776c3340f9d Mon Sep 17 00:00:00 2001 From: Kenfe-Mickael Laventure Date: Thu, 29 Sep 2016 14:43:50 -0700 Subject: [PATCH] Ensure we log into logrus on command error `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 --- main.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/main.go b/main.go index 54065657..1cb8f4db 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "io" "os" "strings" @@ -129,7 +130,20 @@ 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. + cli.ErrWriter = &FatalWriter{cli.ErrWriter} if err := app.Run(os.Args); err != nil { fatal(err) } } + +type FatalWriter struct { + cliErrWriter io.Writer +} + +func (f *FatalWriter) Write(p []byte) (n int, err error) { + logrus.Error(string(p)) + return f.cliErrWriter.Write(p) +}