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 <[email protected]>
  • Loading branch information
mlaventure committed Sep 30, 2016
1 parent 3597b7b commit 924f958
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 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 @@ -129,7 +130,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.
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) {
msg := string(p)
logrus.Error(msg)
fmt.Fprint(f.cliErrWriter, msg)
return len(p), nil
}

0 comments on commit 924f958

Please sign in to comment.