-
Notifications
You must be signed in to change notification settings - Fork 188
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
CLI: Respect config log levels if --verbosity
not explicitly passed
#5925
CLI: Respect config log levels if --verbosity
not explicitly passed
#5925
Conversation
@astamminger tentative solution for the problem you reported. Haven't fully tested everything yet, but at least it seems to satisfy your requirement that the |
4758c43
to
283763e
Compare
Hi @sphuber, thanks for the effort! Haven't had the time yet to test your implementation, but I want to leave you some feedback. From a first sight this looks very reasonable to me. Basically that's a clearly laid-out version of the hacky workaround I was using in my AiiDA installation here to preserve the aiida-loglevel :) Cheers |
283763e
to
e82c701
Compare
Anyone else want to have a look at these changes? @unkcpz @ramirezfranciscof @ltalirz ? |
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.
Thanks @sphuber, it is still not very clear to me the loglevel hierarchy. I assume there are following different levels of the logger,
- the
-v
or--verbosity
which will override all other loglevel (or vise versa it will be override?) - the loglevel defined in config, e.g
logging.aiida_loglevel
.. - The sub-level e.g
aiida.cmdline
. - The loglevel for specific modules or thirdparty packages.
Can you give me a brief overview of which loglevel goes first and how they will override each other?
aiida/cmdline/params/options/main.py
Outdated
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.
Can you check the docstring of function set_log_level
? I am a bit confused since it is not fully conformed with the PR description. If I understand correctly, this method is related only to verdi_loglevel
solely and became the most base log_level can be override by other log_level defined for other modules?
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.
Yes, it is incorrect currently, I will update 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.
Thanks! clear.
The base configuration defines a number of loggers ( Now all these loggers are independent, because each logger has its own "root" namespace. So all these loglevels are also independent and don't affect one another. If Python code does: logger = logging.getLogger('aiida')
logger.warning('warning) The loglevel of the Now, when a The one open question is whether we should simply override the handler for all loggers, and not just The final step is when a user actually explicitly defines a value for This is the behavior that we want. When using
While typing this, I think I should simply update the PR to have the option affect all loggers, and not just |
1fbc300
to
d0e1e07
Compare
I don't have a strong opinion on this. But from my experience long ago debugging things of plumpy, what I expect is the loglevel of plumpy is only controlled by EDIT: it seems you already change it to affect all loggers. No problem with this, I'll have a look. |
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.
@sphuber thanks, just a minor request. All good from my side.
aiida/cmdline/params/options/main.py
Outdated
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.
Thanks! clear.
|
||
monkeypatch.setattr(storage, 'maintain', mock_maintain) | ||
|
||
with caplog.at_level(logging.INFO): |
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.
It was INFO level, but in new code it seems the default level REPORT
is used?
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, the INFO level was not necessary, because all the messages that are being tested for are anyway logged on the REPORT level, so we can use the default. That's why the test also simply pass even after removing the caplog.at_level line
# If ``--verbosity`` is not explicitly defined, values from the config options should be used. | ||
result = run_cli_command(cmd, raises=True, use_subprocess=False) | ||
verify_log_output(result.output, logging.ERROR, logging.WARNING) | ||
|
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.
Can you add a test to show the handler of aiida logger is set to cli?
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.
Added in last commit
d0e1e07
to
c77e7b9
Compare
c77e7b9
to
1e4aa15
Compare
The config allows to define the default log level for the logger of the Python API through the `logging.aiida_loglevel` option. The `verdi` CLI provides the `--verbosity` option to control the level of log messages written to console by the command itself, but also by the API code that is called by it. It does so by overriding the `logging.aiida_loglevel` option. The problem was that it was always doing this, even if the `--verbosity` option was not explicitly specified. The reason is that the option defines `REPORT` as the default. This means the callback is always called and so the `CLI_LOG_LEVEL` global would always be set, causing the `configure_logging` to override the config setting with `REPORT`. The solution is to remove the default from the option. If the option is not explicitly defined, the callback receives `None` and it doesn't set the `CLI_LOG_LEVEL`. This change brings a new problem though, as now, when `verdi` is called without `--verbosity` the `configure_logging` will not actually configure the handlers of the `aiida` logger to be the `CliHandler`. To solve this problem, a new global `aiida.common.log.CLI_ACTIVE` is added which is `None` by default. The callback of the verbosity option sets this to `True` to indicate that we are in a `verdi` call. The `configure_logging` will check this global and if set will replace the `console` handler of all loggers to the `cli` handler.` Finally, a new config option `logging.verdi_loglevel` is introduced. This is necessary because since the default for `--verbosity` has been removed, it means the default from `logging.aiida_loglevel` would be taken. However, since this could be set to something higher than `REPORT`, e.g. `WARNING`, it would mean that most of the verdi commands would not print anything, which would suprise a lot of users. The logger used by `aiida.cmdline.utils.echo` is changed from `aiida.cmdline` to `verdi`, to decouple it from the `aiida` logger. Its default log level is defined by the `logging.verdi_loglevel`, which is overridden if a more specific level is set with the `--verbosity` option.
1e4aa15
to
1f4082c
Compare
Fixes #5923
The config allows to define the default log level for the logger of the Python API through the
logging.aiida_loglevel
option. Theverdi
CLI provides the--verbosity
option to control the level of log messages written to console by the command itself, but also by the API code that is called by it. It does so by overriding thelogging.aiida_loglevel
option.The problem was that it was always doing this, even if the
--verbosity
option was not explicitly specified. The reason is that the option definesREPORT
as the default. This means the callback is always called and so theCLI_LOG_LEVEL
global would always be set, causing theconfigure_logging
to override the config setting withREPORT
.The solution is to remove the default from the option. If the option is not explicitly defined, the callback receives
None
and it doesn't set theCLI_LOG_LEVEL
. This change brings a new problem though, as now, whenverdi
is called without--verbosity
theconfigure_logging
will not actually configure the handlers of theaiida
logger to be theCliHandler
.To solve this problem, a new global
aiida.common.log.CLI_ACTIVE
is added which isNone
by default. The callback of the verbosity option sets this toTrue
to indicate that we are in averdi
call. Theconfigure_logging
will check this global and if set will replace theconsole
handler of theaiida
logger to thecli
handler.`Finally, a new config option
logging.verdi_loglevel
is introduced. This is necessary because since the default for--verbository
has been removed, it means the default fromlogging.aiida_loglevel
would be taken. However, since this could be set to something higher thanREPORT
, e.g.WARNING
, it would mean that most of the verdi commands would not print anything, which would suprise a lot of users.The logger used by
aiida.cmdline.utils.echo
is changed fromaiida.cmdline
toverdi
, to decouple it from theaiida
logger. Its default log level is defined by thelogging.verdi_loglevel
, which is overridden if a more specific level is set with the--verbosity
option.