[Django] #31537: Optionally log server_time with timzone

32 views
Skip to first unread message

Django

unread,
May 4, 2020, 3:18:11 PM5/4/20
to django-...@googlegroups.com
#31537: Optionally log server_time with timzone
-----------------------------------------+------------------------
Reporter: thenewguy | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
It would be helpful if logging format included the timezone for the logged
time. When this information is parsed (for example logging aggregation
using logstash and the elastic stack) the timezone data would be very
helpful so the time is correct.

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

--
Ticket URL: <https://code.djangoproject.com/ticket/31537>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 4, 2020, 3:25:23 PM5/4/20
to django-...@googlegroups.com
#31537: Optionally log server_time with timezone
-------------------------------+--------------------------------------

Reporter: thenewguy | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/31537#comment:2>

Django

unread,
May 4, 2020, 3:25:57 PM5/4/20
to django-...@googlegroups.com
#31537: Optionally log server_time with timzone
-------------------------------+--------------------------------------

Reporter: thenewguy | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by thenewguy:

Old description:

> It would be helpful if logging format included the timezone for the
> logged time. When this information is parsed (for example logging
> aggregation using logstash and the elastic stack) the timezone data would
> be very helpful so the time is correct.
>
> 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

New description:

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.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/31537#comment:1>

Django

unread,
May 4, 2020, 3:29:36 PM5/4/20
to django-...@googlegroups.com
#31537: Optionally log server_time with timezone
-------------------------------+--------------------------------------

Reporter: thenewguy | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by thenewguy:

Old description:

> 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.

New description:

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

--

--
Ticket URL: <https://code.djangoproject.com/ticket/31537#comment:3>

Django

unread,
May 5, 2020, 9:59:38 AM5/5/20
to django-...@googlegroups.com
#31537: Optionally log server_time with timezone
-------------------------------+--------------------------------------

Reporter: thenewguy | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by thenewguy):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/31537#comment:4>

Django

unread,
May 5, 2020, 10:34:07 AM5/5/20
to django-...@googlegroups.com
#31537: Optionally log server_time with timezone.
-------------------------------+--------------------------------------
Reporter: thenewguy | Owner: nobody
Type: Uncategorized | Status: closed
Component: Core (Other) | Version: 3.0
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => wontfix
* component: Uncategorized => Core (Other)


Comment:

You can specify a custom
[https://docs.djangoproject.com/en/3.0/topics/logging/#topic-logging-
parts-formatters formatter] in your
[https://docs.djangoproject.com/en/3.0/topics/logging/#using-logging
logging] configuration. IMO we shouldn't change default formatters.

--
Ticket URL: <https://code.djangoproject.com/ticket/31537#comment:5>

Reply all
Reply to author
Forward
0 new messages