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

Filter leveled logging below a set level #250

Closed
bishoprook opened this issue May 5, 2016 · 3 comments
Closed

Filter leveled logging below a set level #250

bishoprook opened this issue May 5, 2016 · 3 comments

Comments

@bishoprook
Copy link

Apologies if this is something that's been dealt with before but I couldn't find it in past issues or PRs.

If I set up a leveled logger and do some leveled log statements e.g.:

logger := levels.New(log.NewJSONFormatter(os.Stdout))
logger.Debug().Log("msg", "some debug stuff")
logger.Error().Log("err", "got an error")
logger.Info().Log("action", "user action was taken")

I would want the ability to set a log level above which I receive the log statements but below which they're discarded. Something like:

logger.LogLevel(levels.Error)
logger.Debug().Log("msg", "more debug stuff")  // now doesn't log anything

This saves a bit of CPU and I/O cost. It's a feature that log15 had, seems it just never came over here.

Also, you could prevent the evaluation of Valuers, so I can do:

logger.Debug().Log("wowza", func() inferface{} { return someExpensiveOp() })

...but if the log level is set to Info or above, not only does this line not get evaluated, someExpensiveOp never gets called.

I'm game to do the work and send the PR if this sounds like something desirable!

@ChrisHines
Copy link
Member

@bishoprook I would like to see a good implementation of this idea in the log/levels package. I will gladly review a PR along these lines.

The approach taken by log15 is to filter the log events after they enter the logging stack with log15.LvlFilterHandler. Taking that approach allows for an interesting composition with log15/ext.EscalateErrHandler which can change the level of a log event based on other fields, potentially allowing it to pass the level filter anyway.

Implementing level filtering directly in log/levels.Levels allows less flexibility, but provides the opportunity to do less work for filtered events.

My preference is for the first approach but that could change once we see what the code looks like.

@bishoprook
Copy link
Author

See the PR I just submitted and let me know what you think. :)

@peterbourgon
Copy link
Member

This is now accomplished with level.Options.

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

No branches or pull requests

3 participants