Skip to content

how to overwrite timestamp of log entry via python stdlib logging #1078

@nioncode

Description

@nioncode

We want to modify the timestamp of a specific log entry to backdate it to the time when the http request was received instead of when the log gets actually written, as this seems to be the way http access logs should be formatted.

However, we were unable to do so via python's stdlib logging interface. We tried setting the timestamp via the extra field as described in the docs, but this had no effect.

Can you give us a quick example how to modify the timestamp of a single log entry while using the stdlib logging interface? It seems to be possible to simply set the timestamp directly when logging via log_struct as described in #542, but we'd like to keep using the python stdlib's logging interface.

Environment details

  • OS type and version: Cloud Run, container image ubuntu-minimal-2404-lts-amd64
  • Python version: Python 3.12.12
  • pip version: pip 25.0.1
  • google-cloud-logging version: 3.11.2

Code example

logging.info("hello", extra={"timestamp": timestamp})

Metadata

Metadata

Assignees

No one assigned

    Labels

    api: loggingIssues related to the googleapis/python-logging API.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions