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

Ensure we log into logrus on command error #1089

Merged
merged 1 commit into from
Oct 3, 2016

Conversation

mlaventure
Copy link
Contributor

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

@mlaventure mlaventure changed the title Ensure we log into logus on command error Ensure we log into logrus on command error Sep 29, 2016
@mlaventure
Copy link
Contributor Author

So, urfave/cli also print the normal error message via ErrWriter, unfortunately there is no way to make a distinction between the provenance of the error from the io.Writer interface.

I've gotta go for the day, but I'll look at it tomorrow, if someone has an idea on how to handle both, let me know :)

@mlaventure
Copy link
Contributor Author

I just realized, since this is only called on error from the cli, I can just also output it to stderr :)

@mlaventure mlaventure force-pushed the fix-logging-on-error branch 2 times, most recently from cb9e9c7 to de4dd42 Compare September 30, 2016 14:45
// 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{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: instead of the global originalCliErrWriter add it to a field on FatalWriter

@mlaventure
Copy link
Contributor Author

updated

On Fri, Sep 30, 2016 at 9:32 AM Michael Crosby notifications@github.com
wrote:

@crosbymichael commented on this pull request.

In main.go
#1089 (review)
:

@@ -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{}

nit: instead of the global originalCliErrWriter add it to a field on
FatalWriter


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#1089 (review),
or mute the thread
https://github.com/notifications/unsubscribe-auth/APUifvmghyrQObp9gwylp__08ip-T2hxks5qvTmHgaJpZM4KKhz7
.

@crosbymichael
Copy link
Member

crosbymichael commented Sep 30, 2016

LGTM

Approved with PullApprove

@mlaventure
Copy link
Contributor Author

ping @cyphar @mrunalp

It's a small change, but it's preventing us to vendor a newer version of runc into docker :)

msg := string(p)
logrus.Error(msg)
fmt.Fprint(f.cliErrWriter, msg)
return len(p), nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just be f.cliErrWriter.Write(p) IMO. Why go through Fprintf (which in languages like C would be punished with arbitrary code execution 😉).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other words just make the code:

logrus.Error(string(p))
return f.cliErrWriter.Write(p)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, totally right. Update incoming, thanks.

@mlaventure mlaventure force-pushed the fix-logging-on-error branch from 924f958 to ed9767b Compare October 3, 2016 14:53

func (f *FatalWriter) Write(p []byte) (n int, err error) {
logrus.Error(string(p))
f.cliErrWriter.Write(p)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you intentionally ignoring the error / actual number of bytes written?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was initially because logrus doesn't return it. But I guess it makes sense to return the value from the cli writer

`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>
@mlaventure mlaventure force-pushed the fix-logging-on-error branch from ed9767b to 294d24f Compare October 3, 2016 15:01
@cyphar
Copy link
Member

cyphar commented Oct 3, 2016

LGTM.

Approved with PullApprove

@mrunalp
Copy link
Contributor

mrunalp commented Oct 3, 2016

LGTM

Approved with PullApprove

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants