Skip to content

Commit 02f8fa7

Browse files
author
Mrunal Patel
authored
Merge pull request #1089 from mlaventure/fix-logging-on-error
Ensure we log into logrus on command error
2 parents 7b1bcb3 + 294d24f commit 02f8fa7

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"io"
56
"os"
67
"strings"
78

@@ -129,7 +130,20 @@ func main() {
129130
}
130131
return nil
131132
}
133+
// If the command returns an error, cli takes upon itself to print
134+
// the error on cli.ErrWriter and exit.
135+
// Use our own writer here to ensure the log gets sent to the right location.
136+
cli.ErrWriter = &FatalWriter{cli.ErrWriter}
132137
if err := app.Run(os.Args); err != nil {
133138
fatal(err)
134139
}
135140
}
141+
142+
type FatalWriter struct {
143+
cliErrWriter io.Writer
144+
}
145+
146+
func (f *FatalWriter) Write(p []byte) (n int, err error) {
147+
logrus.Error(string(p))
148+
return f.cliErrWriter.Write(p)
149+
}

0 commit comments

Comments
 (0)