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

support sub-second granularity/precision in --log-date-format (strftime %f) #10991

Closed
jtmoon79 opened this issue May 12, 2023 · 1 comment · Fixed by #11047
Closed

support sub-second granularity/precision in --log-date-format (strftime %f) #10991

jtmoon79 opened this issue May 12, 2023 · 1 comment · Fixed by #11047
Labels
good first issue easy issue that is friendly to new contributor plugin: logging related to the logging builtin plugin topic: config related to config handling, argument parsing and config file type: enhancement new feature or API change, should be merged into features branch

Comments

@jtmoon79
Copy link

jtmoon79 commented May 12, 2023

tl;dr pytest processing strftime %f specifier from --log-*-date-format arguments would allow me to accurately merge log messages from disparate sub-systems

What's the problem?

Tests I run have pytest log messages that print at the second granularity for the datetimestamp, e.g. 2023-05-11T13:45:34. At the same time, other log file messages not generated by pytest print sub-second datetimestamps, e.g. 2023-05-11T13:45:34.123.

When reviewing the various logs, there are many message from other system components that are printing many log messages per second. Because pytest log messages are lacking sub-second precision, I am unable to align pytest log messages within other system log messages.

contrived example

For example, the system-under-test generates a log file like:

2023-05-11T13:45:34.001 starting the frobulator
2023-05-11T13:45:34.100 wiggling the waggulator
2023-05-11T13:45:34.200 stopping the frobulator
2023-05-11T13:45:34.301 starting the frobulator
2023-05-11T13:45:34.400 poking the prokulator
2023-05-11T13:45:34.450 prokulator response ERROR_NOT_ONLINE
2023-05-11T13:45:34.500 stopping the frobulator
2023-05-11T13:45:34.600 starting the frobulator
2023-05-11T13:45:34.700 juggling some bowling pins
2023-05-11T13:45:34.750 DROPPED A PIN!
2023-05-11T13:45:34.800 stopping the frobulator
2023-05-11T13:45:34.839 ERROR 0x0F009001 STOPPING THE frobulator

and the driver of tests, pytest, generates a log file like:

2023-05-11T13:45:34 checking device
2023-05-11T13:45:34 ping device
2023-05-11T13:45:34 device error!

The pytest log messages cannot be precisely ordered among the other log messages that occurred during the datetime second 2023-05-11T13:45:34, there were many things that occurred in the other system components within that second.

current confusion

Given the following pytest code

import logging
import pytest

logging.basicConfig()
logger = logging.getLogger(__name__)

def test_logger():
    logger.error("test_logger()ERROR")
    logger.warning("test_logger()WARNING")

To add sub-second granularity, it seems sensible to add %f within the --log-cli-date-format

$ python -m pytest \
         -v -v \
         --log-cli-date-format="%Y%m%dT%H%M%S.%f" \
         --capture=tee-sys \
         -k "test_logger"

but then I see the confusing output of

20230511T181007.%f: ERROR : [test_main.py:27 - test_logger()] : test_logger()ERROR
20230511T181007.%f: WARNING : [test_main.py:28 - test_logger()] : test_logger()WARNING

pytest logging is ignoring the strftime %f specifier!


pytest feature request

I want pytest log messages to print sub-second granularity, e.g. process strftime %f within --log-date-format="...%f..." settings.

Describe the solution you'd like

Support strftime %f specifier in the various settings for date-format, e.g. --log-date-format, --log-cli-date-format, --log-file-date-format.

In my complex testing system, this means all log messages would be printed to millisecond precision. This allows engineers investigating issues to more accurately merge disparate testing system logs by their natural ordering mechanism of a datetimestamp.


Alternative Solutions

I can set the logging format to include %(msecs)03d.
However, it's a little confusing to have to manipulate log datetimestamps by two different mechanisms, --log-cli-format and --log-cli-date-format.

example workaround

On the command-line run:

$ python -m pytest \
         -v -v \
         --log-cli-date-format="%Y%m%dT%H%M%S." \
         --log-cli-format="%(asctime)s%(msecs)03d: %(levelname)s : [%(filename)s:%(lineno)s - %(funcName)s()] : %(message)s" \
         --capture=tee-sys \
         -k "test_logger"

This prints datetimestamps with millisecond precision

20230511T180748.192: ERROR : [test_main.py:27 - test_logger()] : test_logger()ERROR
20230511T180748.195: WARNING : [test_main.py:28 - test_logger()] : test_logger()WARNING

Summary

It is more intuitive for pytest to process the Python strftime %f specifier within all --*-date-format options.

@jtmoon79 jtmoon79 changed the title support sub-second granularity/precision for in --log-date-format (strftime %f) support sub-second granularity/precision in --log-date-format (strftime %f) May 12, 2023
@jtmoon79 jtmoon79 changed the title support sub-second granularity/precision in --log-date-format (strftime %f) support sub-second granularity/precision in --log-date-format (strftime %f) May 12, 2023
@Zac-HD Zac-HD added type: enhancement new feature or API change, should be merged into features branch good first issue easy issue that is friendly to new contributor topic: config related to config handling, argument parsing and config file plugin: logging related to the logging builtin plugin labels May 16, 2023
theirix added a commit to theirix/pytest that referenced this issue May 30, 2023
Removed default constructor and documentation for the internal class.

Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
@jtmoon79
Copy link
Author

jtmoon79 commented Jun 4, 2023

Wow, fantastic! Looking forward to this release! Thanks everyone! @Zac-HD @theirix @nicoddemus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue easy issue that is friendly to new contributor plugin: logging related to the logging builtin plugin topic: config related to config handling, argument parsing and config file type: enhancement new feature or API change, should be merged into features branch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants