Pytest logging ignores pytest.ini log_cli_format for captured logs

Viewed 294

I have a pytest.ini file as such:

log_cli = 1
log_cli_level = INFO
log_cli_format = [%(levelname)s]: %(message)s
;log_file = ./Logs/pytest_.log
log_file_level = INFO
log_file_format = %(asctime)s [%(levelname)s]: %(message)s
log_file_date_format=%Y-%m-%d %H:%M:%S

When executing, the live logs and the file logs follow the given format, but there are also "Captured logs" that are printed and they do not follow the given format. Is there any way to modify the format of the "Captured Log". Here is an example output:

------- live log setup ---------
[INFO]: Using local chrome browser....
[INFO]: Successfully logged in
[INFO]: Click Customer Success
[INFO]: The Dashboard Loaded Successfully 


--- Captured log setup ---------
INFO     conftest:conftest.py:46 Using local chrome browser....
INFO     conftest:conftest.py:125 Successfully logged in
INFO     page_objects.CommonPageObjects:CommonPageObjects.py:292 Click Customer Success
INFO     page_objects.CommonPageObjects:CommonPageObjects.py:300 The Dashboard Loaded Successfully
1 Answers

You need to pass the same format string via --log-format as specified in log_cli_format, or set the log_format option in your pytest.ini/pyproject.toml. Example for pyproject.toml:

[tool.pytest.ini_options]
log_cli = true
log_cli_level = "INFO"
log_format = "[%(levelname)s]: %(message)s"
log_cli_format = "[%(levelname)s]: %(message)s"

The same applies for the date format, should you want to include timestamps via the asctime keyword. Set log_date_format to the same format string as log_cli_date_format or pass it via --log-date-format from command line. For example:

log_date_format = "%Y-%m-%d %H:%M:%S"
log_cli_date_format = "%Y-%m-%d %H:%M:%S"
Related