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 option to increase the level of a logger #775

Merged
merged 5 commits into from
Jan 30, 2020
Merged

Conversation

prashantv
Copy link
Collaborator

We've had a few requests on how to change the level of a logger. With
the current APIs, it's not possible to reduce the log level (e.g., go
from Info to Debug), but it is possible to increase the level using
WrapCore with a custom filtering core.

Since it's a common request, I think we should add an option to make
this easy to use.

I want to make sure "increase" is part of the API to avoid confusion on
whether it can reduce the log level.

Fixes #774.

@codecov
Copy link

codecov bot commented Dec 23, 2019

Codecov Report

Merging #775 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #775      +/-   ##
==========================================
+ Coverage   98.19%   98.21%   +0.01%     
==========================================
  Files          42       43       +1     
  Lines        2275     2293      +18     
==========================================
+ Hits         2234     2252      +18     
  Misses         33       33              
  Partials        8        8
Impacted Files Coverage Δ
options.go 100% <100%> (ø) ⬆️
zapcore/increase_level.go 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 73a5f64...a36ef68. Read the comment docs.

@weeco
Copy link

weeco commented Dec 23, 2019

I think this is related to #581 , but when I got the author correctly he wishes an option to change the loglevel on a per message basis rather than just having an easy way to create a new child logger which prints at a different log level.

I'll leave this note here and reference here, so that issue as it might help some of the users stumbling across #581

Copy link
Collaborator

@abhinav abhinav left a comment

Choose a reason for hiding this comment

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

Do we have any way to guard against attempts to lower the level?

I'm concerned that despite naming, this will be very easy to misuse.

// NewLevelCore creates a core that can be used to increase the level of an existing
// Core. It cannot be used to decrease the logging level, as it acts as a filter
// before calling the underlying core.
func NewLevelCore(core Core, level LevelEnabler) Core {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I suspect this needs a similar name that implies increasing levels only.

@prashantv
Copy link
Collaborator Author

Do we have any way to guard against attempts to lower the level?

I'm concerned that despite naming, this will be very easy to misuse.

We can't protect against it, but we can access the errorOutput for the logger, and write a message to it if we detect that someone tries to lower the log level. I don't think adding errors to the API will be very ergonomic. We could do a DPanic but that might be a little surprising. Any other ideas?

@abhinav
Copy link
Collaborator

abhinav commented Jan 7, 2020

Logging to errorOutput seems reasonable. I agree that having this error out
would be less ergonomic.

We've had a few requests on how to change the level of a logger. With
the current APIs, it's not possible to reduce the log level (e.g., go
from Info to Debug), but it is possible to increase the level using
`WrapCore` with a custom filtering core.

Since it's a common request, I think we should add an option to make
this easy to use.

I want to make sure "increase" is part of the API to avoid confusion on
whether it can reduce the log level.

Fixes #774.
@prashantv prashantv changed the title [WIP] Add option to increase the level of a logger Add option to increase the level of a logger Jan 24, 2020
@prashantv
Copy link
Collaborator Author

Updated with more explicit name + error if the level is decreased. Could definitely improve the message so looking for suggestions

Copy link
Collaborator

@abhinav abhinav left a comment

Choose a reason for hiding this comment

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

New APIs look fine. Minor comments.

zapcore/increase_level.go Show resolved Hide resolved
zapcore/increase_level_test.go Outdated Show resolved Hide resolved
logger_test.go Outdated Show resolved Hide resolved
increase_level_test.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@abhinav abhinav left a comment

Choose a reason for hiding this comment

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

LGTM besides prior comments

prashantv and others added 3 commits January 29, 2020 21:21
Co-Authored-By: Abhinav Gupta <abg@uber.com>
Co-Authored-By: Abhinav Gupta <abg@uber.com>
@prashantv prashantv merged commit 127ea09 into master Jan 30, 2020
@prashantv prashantv deleted the increase_level branch January 30, 2020 05:46
cgxxv pushed a commit to cgxxv/zap that referenced this pull request Mar 25, 2022
We've had a few requests on how to change the level of a logger. With
the current APIs, it's not possible to reduce the log level (e.g., go
from Info to Debug), but it is possible to increase the level using
`WrapCore` with a custom filtering core.

Since it's a common request, I think we should add an option to make
this easy to use.

I want to make sure "increase" is part of the API to avoid confusion on
whether it can reduce the log level.

Fixes uber-go#774.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Change LogLevel without creating a new core
3 participants