Opened 4 years ago

Closed 4 years ago

#31537 closed Uncategorized (wontfix)

Optionally log server_time with timezone.

Reported by: thenewguy Owned by: nobody
Component: Core (Other) Version: 3.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by thenewguy)

It would be helpful if logging format included the timezone for the logged time. When this information is parsed (for example log aggregation via logstash) the timezone data would be very helpful to create a consistent timestamp.

For reference, https://docs.python.org/3/library/logging.html

Changed in version 3.3: Previously, the default format was hard-coded as in this example: 2010-09-06 22:38:15,292 where the part before the comma is handled by a strptime format string ('%Y-%m-%d %H:%M:%S'), and the part after the comma is a millisecond value. Because strptime does not have a format placeholder for milliseconds, the millisecond value is appended using another format string, '%s,%03d' — and both of these format strings have been hardcoded into this method. With the change, these strings are defined as class-level attributes which can be overridden at the instance level when desired. The names of the attributes are default_time_format (for the strptime format string) and default_msec_format (for appending the millisecond value).

Since the date time output is not created via one format operation it is more complicated than just adding %Z or %z to the format string.

Happy to do the work. Please provide feedback/direction.

It seems like the simplest way to do this would be to always log in UTC and add the "Z" for timezone. But +-offset could also be used if localtime needs to be supported

Change History (5)

comment:1 by thenewguy, 4 years ago

Description: modified (diff)

comment:2 by thenewguy, 4 years ago

Summary: Optionally log server_time with timzoneOptionally log server_time with timezone

comment:3 by thenewguy, 4 years ago

Description: modified (diff)

comment:4 by thenewguy, 4 years ago

This is an example of how the ServerFormatter can be made to output server_time as somthing like 2020-05-05 13:12:39,637Z or 2020-05-05T13:12:39.637Z if you change the format string

from time import gmtime

from django.utils.log import ServerFormatter


class UTCServerFormatter(ServerFormatter):
    default_time_format = '%Y-%m-%d %H:%M:%S'
    default_msec_format = '%s,%03dZ'
    converter = gmtime

This is fairly easy but took a fair amount of source code reading to put together. I think it would be a good idea to be more flexible so I am going to leave the issue open.

comment:5 by Mariusz Felisiak, 4 years ago

Component: UncategorizedCore (Other)
Resolution: wontfix
Status: newclosed
Summary: Optionally log server_time with timezoneOptionally log server_time with timezone.

You can specify a custom formatter in your logging configuration. IMO we shouldn't change default formatters.

Note: See TracTickets for help on using tickets.
Back to Top