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

Added WithError(err) #179

Merged
merged 3 commits into from
Sep 6, 2015
Merged

Added WithError(err) #179

merged 3 commits into from
Sep 6, 2015

Conversation

yawn
Copy link
Contributor

@yawn yawn commented May 13, 2015

This is very small feature request for a function of logrus I use all the time: adding an error message to the context.

@sirupsen
Copy link
Owner

This is not unreasonable. I'll think a bit about this.

@yawn
Copy link
Contributor Author

yawn commented May 13, 2015

👍

@yawn
Copy link
Contributor Author

yawn commented May 18, 2015

WDYT about implementing Error in *Entry? This could be useful when we'd change the return value of the actual logging methods from void to *Entry, allowing idioms like this:

if err != nil {
  return ctx.WithError(err).Error("This did not work")
}

@yawn
Copy link
Contributor Author

yawn commented May 18, 2015

PR is missing functionality in exported. I'll add that if you like the functionality.

@sirupsen
Copy link
Owner

@yawn isn't that what you've already done? re adding to *Entry?

I've also decided that this is going in. It should be added to Exported with an accompanying test.

@yawn
Copy link
Contributor Author

yawn commented May 19, 2015

I meant

  1. implementing the Error interface in *Entry and
  2. returning Entry in Debug, Error etc.

This way we could return errors logged to the context by returning the call to the logging method. I can prepare an example if you want.

@sirupsen
Copy link
Owner

Yeah, I think that's valid. I've wanted to do that before. Logging is often awkward in Go because it can be unclear when to log and when to return an error, and sometimes you even want to do both—because the context you have is not present. But fewer log lines with more information is often better than a lot of log lines with little information, and makes the code harder to read.

It should be a separate API, but I'm open to the idea.

@sirupsen
Copy link
Owner

If you could add this to exported I'll get it merged.

@yawn
Copy link
Contributor Author

yawn commented May 19, 2015

I'll do the additions on the train. Regarding the API:

  • I think it would not hurt to return the *Entry that log message was created on - currently it's void so, nothing changes IMHO
  • Did you have a look at my proposal Proposal: log events #182?

@yawn
Copy link
Contributor Author

yawn commented May 19, 2015

Done, with the proposed API (756db3c) as separate commit.

@yawn
Copy link
Contributor Author

yawn commented Jul 1, 2015

Do you need anything more from me here @sirupsen?

@aybabtme
Copy link
Collaborator

aybabtme commented Sep 6, 2015

LGTM

aybabtme added a commit that referenced this pull request Sep 6, 2015
@aybabtme aybabtme merged commit c639bed into sirupsen:master Sep 6, 2015
@imax9000
Copy link

imax9000 commented Sep 6, 2015

This just broke the build of https://github.com/docker/distribution/tree/master/context

../github.com/docker/distribution/context/http.go:184: cannot use l (type *logrus.Entry) as type Logger in return argument:
    *logrus.Entry does not implement Logger (wrong type for Debug method)
        have Debug(...interface {}) *logrus.Entry
        want Debug(...interface {})

@aybabtme
Copy link
Collaborator

aybabtme commented Sep 6, 2015

@gelraen thanks for letting me know, it seems this interface should be updated:

https://github.com/docker/distribution/blob/master/context/logger.go#L9-L40

The project uses vendored dependencies so it should not be affected by the change. I'll send them a PR anyways.

aybabtme added a commit to aybabtme/distribution that referenced this pull request Sep 6, 2015
A recent change in logrus broke the expectations of the
`context.Logger` interface. We can enforce the expectation
by wrapping the `*logrus.Entry` in a type that matches this
interface.

Logrus change: sirupsen/logrus#179

Signed-off-by: Antoine Grondin <[email protected]>
@imax9000
Copy link

imax9000 commented Sep 6, 2015

@aybabtme please also note that this breaks promise of being drop-in replacement for log package (https://github.com/Sirupsen/logrus/blob/master/README.md#example)

@aybabtme
Copy link
Collaborator

aybabtme commented Sep 6, 2015

It doesn't break the promise for the log.Logger, only if you use the *log.Entry as a log.Logger.

I can't say what the intent was with regard to compatibility, I'll let @sirupsen decide if he sees it as applying to *log.Entry. If so, we can revert the merge.

@yawn
Copy link
Contributor Author

yawn commented Sep 7, 2015

I'd vote for a partial revert of 756db3c then ("changing the return value"). This is not strictly necessary (except for the cast-to-error usecase) for this PR but is probably useful in any case. It's just seems to break compatibility ...

@aybabtme aybabtme mentioned this pull request Sep 8, 2015
@sirupsen
Copy link
Owner

sirupsen commented Sep 8, 2015

Fortunately this was never released as a version. Thanks for your quick revert @aybabtme.

devopstaku pushed a commit to devopstaku/logrus that referenced this pull request Aug 9, 2016
cgxxv pushed a commit to cgxxv/logrus that referenced this pull request Mar 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants