Skip to content

Add microseconds to logging.LogRecord #90450

Open

Description

@jamescasbon
BPO 46292
Nosy @vsajip, @jamescasbon
Files
  • usecs.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2022-01-07.14:11:50.738>
    labels = ['type-feature', 'library', '3.11']
    title = 'Add microseconds to logging.LogRecord'
    updated_at = <Date 2022-01-12.15:23:32.019>
    user = 'https://github.com/jamescasbon'

    bugs.python.org fields:

    activity = <Date 2022-01-12.15:23:32.019>
    actor = 'arwade'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2022-01-07.14:11:50.738>
    creator = 'jamescasbon'
    dependencies = []
    files = ['50546']
    hgrepos = []
    issue_num = 46292
    keywords = ['patch']
    message_count = 2.0
    messages = ['409963', '410011']
    nosy_count = 3.0
    nosy_names = ['vinay.sajip', 'arwade', 'jamescasbon']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue46292'
    versions = ['Python 3.11']

    Activity

    1. jamescasbon commented on Jan 7, 2022

      jamescasbonmannequin
      MannequinAuthor

      LogRecord makes microseconds available via the msecs attribute. This patch adds microseconds via usecs attribute.

      Some regulators (eg MIFID II) require accuracy greater than 1ms in some industries.

      This patch calls time_ns rather than time so that the usecs and msecs are calculated from int types and then gets the original ct attribute via division.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Jan 7, 2022
    3. vsajip commented on Jan 7, 2022

      @vsajip
      Member

      Thanks for the patch. We're need to get contributors to sign a Contributor License Agreement (CLA) before we can accept their patches. Would you be willing to do this? The process could be smoother, but it's not too bad, and here's where you get started:

      https://www.python.org/psf/contrib/contrib-form/

    4. transferred this issue fromon Apr 10, 2022
    5. moved this to CLA not signed in Logging issues 🪵on Aug 26, 2022
    6. askoretskiy commented on Jul 5, 2024

      @askoretskiy

      Any progress here? That would be really helpful feature. Had to implement third-party Formatter class to solve this issue.

      Maybe, core team could implement something similar? Or maybe even allow "%f" in the dateFormat?

      import logging
      from datetime import datetime
      
      class CustomFormatter(logging.Formatter):
          def formatTime(self, record, datefmt=None):
              if not datefmt:
                  return super().formatTime(record, datefmt=datefmt)
      
              return datetime.fromtimestamp(record.created).astimezone().strftime(datefmt)
      
    7. vsajip commented on Jul 5, 2024

      @vsajip
      Member

      Patch partly superseded by #102412 and #120709. Will look into this shortly.

    8. askoretskiy commented on Feb 2, 2025

      @askoretskiy

      Thanks for merging into Python 3.13.

      How fix is shorter:

      class CustomFormatter(logging.Formatter):
          # Workaround until https://github.com/python/cpython/issues/90450 is fixed
          def formatTime(self, record, datefmt=None):
              record.usecs = 100_000 * (record.created % 1)
              return super().formatTime(record, datefmt=datefmt)

      And usage:

      LOGGING = {
          ...,
          "formatters": {
              "verbose": {
                  "format": "{asctime},{usecs:06.0f} {levelname:8} {name} {message}",
                  "datefmt": "%Y-%m-%d %H:%M:%S",
                  "style": "{",
                  "class": "CustomFormatter",
              },
      }
    9. sayedmasihemami commented on Sep 14, 2026

      @sayedmasihemami

      @askoretskiy should the multiplier in your suggested fix not be 1_000_000 instead?

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions