-
Notifications
You must be signed in to change notification settings - Fork 17
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
fix wrong logging behavior [RHELDST-3788] #42
Conversation
alt_src/alt_src.py
Outdated
@@ -70,6 +70,8 @@ | |||
CONFIG_INT_OPTS = set([]) | |||
CONFIG_BOOL_OPTS = set(['smtp_enabled', 'push_tags', 'debrand']) | |||
|
|||
logging.basicConfig(filename=CONFIG_DEFAULTS["log_file"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two issues with this:
-
this shouldn't be done at the top level like this, because it means even just importing the file is going to change loggers (as is done e.g. by tests). If we'll call logging.basicConfig, then can you please have that inside of "main" somewhere?
-
alt-src already has a
setup_logging
function and I'm not sure these are going to work together correctly. As I understand it, this logging.basicConfig is going to install a default handler, but then in setup_logging, another StreamHandler to sys.stdout is added.
Due to the second problem, I would suspect that this could result in duplicate log messages, i.e. some logs coming out twice instead of once. I haven't tested it though. Have you tried it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rohanpm, the trouble is the handlers of a logger are set up after options.config = get_config(options.cfile, options.copts)
,
but the get_config
process can also be corrupted and need a logger to track this.
So if the something wrong happens in get_config
, then there is no handler.
This PR is not ready.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I understand that.
In another comment I've suggested changes to setup_logging which could make this work correctly. As well as that, a couple suggestions here:
- don't pass "filename". What's the point?
CONFIG_DEFAULTS["log_file"]
is alwaysNone
. - I think a comment explaining this would be worthwhile. Like "We don't know the desired log config until we've read the config file, but we might need to log something before then - so ensure there's at least some basic config (to be replaced later)."
updated the commit message to this
|
You need to consider handlers on the root logger too. I pulled this locally and confirmed that there is indeed a duplicate message problem:
I would say the desired setup (which alt-src actually doesn't implement correctly right now) is:
Therefore I think an appropriate way to get this all working would be:
|
git commit message updated
|
3bb0522
to
eb8b3a3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have not reviewed fully, but two points on this before completing review:
- for all those warn => warning changes. Can you please do that in a separate PR? I don't see a reason to combine it into this PR. Note if you did it separately, that could more easily pass review and not be blocked by other matters.
- why change it to use the root logger everywhere rather than the "altsrc" logger as it was before? I think this is not a good change as it makes the code less reusable across environments. For example, maybe we want to run this code directly within Pub and we may want to enable DEBUG logs just for altsrc without enabling them for everything else.
The second point is the main issue here, I don't want to review it fully until that is addressed in case undoing that change somehow breaks it again.
alt_src/alt_src.py
Outdated
@@ -2145,6 +2145,10 @@ def setup_logging(options): | |||
handler = logging.StreamHandler(sys.stdout) | |||
handler.setFormatter(logging.Formatter(options.config['log_format'])) | |||
handler.setLevel(output_log_level) | |||
|
|||
# remove the default handler created by logging.basicConfig in main function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... sorry, I'm having a doubt that this makes sense (or at least the comment).
logger
here is the "altsrc" logger.
logging.basicConfig
is expected to install a handler on the root logger.
So, wouldn't logger.handlers already be empty here? Why would altsrc logger have handlers at all, at this point in the code?
Also: with the current approach then, this is still only fixing the setup correctly for altsrc logger. Messages from other loggers won't end up going to the stream or file handlers here since it's only being installed on the altsrc logger.
This is what I said in an earlier comment:
I would say the desired setup (which alt-src actually doesn't implement correctly right now) is:
- only the root logger has handlers
- it always has exactly one StreamHandler
- it may also have an additional FileHandler (if log_file is set)
I think that's still valid and it'd be preferable to implement it that way.
bb85dd2
to
818d392
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall looks fine, would be nice to fix that trivial formatting issue.
1. Handler configuration based on the result of "get_config" in main function, but if something wrong happens in "get_config", the "logger.handlers" will be empty, and result in the error 'No handlers could be found for logger "altsrc"'. Add logging.basicConfig in main function for root logger's default handler creation, this is used to catch log before "setup_logging" is called. Update logic in setup_logging: only the root logger has handlers. 2. Add an autouse pytest fixture "reset_loggers" in conftest.py. It saves the handlers before each test, then puts them back after end of the test. It keeps the loggers and handlers in every test consistent.
@rohanpm formatting issue fixed |
Missing "logging.basicConfig" resulted in wrong logging behavior,
No handlers could be found for logger "altsrc"
, instead of showingthe real valuable info, e.g. "ERROR:altsrc:Missing config file:".