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

Handle microseconds with custom logging.Formatter #11047

Merged
merged 4 commits into from May 30, 2023

Conversation

theirix
Copy link
Contributor

@theirix theirix commented May 28, 2023

Added handling of %f directive to print microseconds in log format options, such as log-date-format. It is impossible to do with a standard logging.Formatter because it uses time.strftime which doesn't know about milliseconds and %f. In this PR I added a custom Formatter which converts LogRecord to a datetime.datetime object and formats it with %f flag. This behaviour is enabled only if a microsecond flag is specified in a format string.

Also added a few tests to check the standard and changed behaviour.

Closes #10991.

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thnaks a lot @theirix!

Left two minor comments, other than that LGTM. 👍

changelog/10991.improvement.rst Outdated Show resolved Hide resolved
src/_pytest/logging.py Outdated Show resolved Hide resolved
nicoddemus and others added 3 commits May 30, 2023 07:04
Removed default constructor and documentation for the internal class.

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

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @theirix!

@nicoddemus nicoddemus merged commit 4da9026 into pytest-dev:main May 30, 2023
25 checks passed
@theirix
Copy link
Contributor Author

theirix commented May 30, 2023

Glad to help, @nicoddemus !

@twmr
Copy link
Contributor

twmr commented Jun 1, 2023

Thx for the fix, but shouldn't the fix be implemented upstream in the stdlib?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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