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

Add Func log method #321

Merged
merged 1 commit into from May 19, 2021
Merged

Add Func log method #321

merged 1 commit into from May 19, 2021

Conversation

marwatk
Copy link
Contributor

@marwatk marwatk commented May 19, 2021

This is adds the Func log method to log using an anonymous function
only if the level is currently enabled.

The use case is for when you don't own an object and therefore can't
create your own marshaller but need to do some translation prior to
logging.

For example, this:

msg := log.Debug()
if msg.Enabled() {
  msg.Str("complicated_thing", makeBinaryThingLoggable(thing))
}
msg.Msg("Sending complicated thing")

Turns into this:

log.Debug().
  Func(func(e *Event) { e.Str("complicated_thing", makeBinaryThingLoggable(thing)) }).
  Msg("Sending complicated thing")

I considered a StrFunc instead which would be simpler but less flexible. I'm open to either and the possibility I'm missing a much simpler way to accomplish the same thing.

event.go Outdated Show resolved Hide resolved
This is adds the Func log method to log using an anonymous function
only if the level is currently enabled.

The use case is for when you don't own an object and therefore can't
create your own marshaller but need to do some translation prior to
logging.

For example, this:

msg := log.Debug()
if msg.Enabled() {
  msg.Str("complicated_thing", makeBinaryThingLoggable(thing))
}
msg.Msg("Sending complicated thing")

Turns into this:

log.Debug().
  Func(func(e *Event) { e.Str("complicated_thing", makeBinaryThingLoggable(thing)) }).
  Msg("Sending complicated thing")
@rs
Copy link
Owner

rs commented May 19, 2021

Would be nice to add a usage example in the readme next to the other method.

@marwatk
Copy link
Contributor Author

marwatk commented May 19, 2021

Would be nice to add a usage example in the readme next to the other method.

@rs, I'm not 100% sure where you mean. No sections in the readme are jumping out at me as a logical place to put it. Do you have a specific section in mind?

@rs
Copy link
Owner

rs commented May 19, 2021

You're right, I thought the Enable example was in its own section.

@rs rs merged commit ffbd37b into rs:master May 19, 2021
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

2 participants