pytest-odoo
pytest-odoo copied to clipboard
Configuration of log handlers
Pytest adds a new --odoo-log-level
option which is forwarded as odoo's log level.
By default it uses the critical
log level to display the less logs possible. But you can change it when you need more details for debugging a test.
Odoo has 2 options for configuring log levels:
-
--log-level
: a global single level (critical, error, info, debug, ...) -
--log-handler
: a refined log level per logger name (':INFO,odoo.http.rpc.request:INFO')
The first one is based on this config:
{'critical': ['odoo:CRITICAL', 'werkzeug:CRITICAL'],
'debug': ['odoo:DEBUG', 'odoo.sql_db:INFO'],
'debug_rpc': ['odoo:DEBUG', 'odoo.sql_db:INFO', 'odoo.http.rpc.request:DEBUG'],
'debug_rpc_answer': ['odoo:DEBUG', 'odoo.sql_db:INFO', 'odoo.http.rpc:DEBUG'],
'debug_sql': ['odoo.sql_db:DEBUG'],
'error': ['odoo:ERROR', 'werkzeug:ERROR'],
'info': [],
'warn': ['odoo:WARNING', 'werkzeug:WARNING']}
And the second one will be applied after the --log-level
, so it overrides it.
The default log handler is :INFO
.
The result is that when you use a command such as (both are the same):
pytest -s my_addon/tests --odoo-log-level=critical
# means critical log-level
pytest -s my_addon/tests
The log levels will be:
:INFO
odoo:CRITICAL
werkzeug:CRITICAL
As the log levels are recursive, all the loggers below odoo (odoo.addons.sale
, ...) and werkzeug will be on the critical log-level. But all other loggers, such as those used by libs (Pillow, ...) will have an INFO logger by default.
What we should probably do is set a default --log-handler
to :CRITICAL
and add an option to modify it the same way it's done for the log level.
As a workaround, you can use a configuration file (https://github.com/camptocamp/pytest-odoo/blob/33e4e634920a96c4ec9fc043900d53a952350bbd/pytest_odoo.py#L41) and set the log handler configuration into it.
Now, we observed in #15 that even logs in DEBUG of Pillow (and passlib) were shown, and they should have been hidden as the default is INFO.
The reason is that each test is in a context manager (pytest.logging.catching_logs) which, during the duration of the test, sets the root loglevel to zero (0 is below DEBUG, display everything). Predefined children log levels will not be changed, but others will all show debug logs. (introduced at https://github.com/pytest-dev/pytest/issues/3013)
The context manager: https://github.com/pytest-dev/pytest/blob/5ad1313b8a18fa3862623d2360cbeb39c202b838/_pytest/logging.py#L84-L106
Hopefully, it has been fixed in pytest 3.4.2 (https://github.com/pytest-dev/pytest/commit/8dcd2718aa90620d9fdc872f49ba8ccbe9c9dd13)
@guewen I upgraded to pytest 3.5.0 right now and it really fixes this ;)