Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #25692 > unrolled thread
| Started by | "Alex van der Spek" <zdoor@xs4all.nl> |
|---|---|
| First post | 2012-07-20 15:00 +0200 |
| Last post | 2012-07-20 14:50 -0400 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
logging time format millisecond precision decimalsign "Alex van der Spek" <zdoor@xs4all.nl> - 2012-07-20 15:00 +0200
Re: logging time format millisecond precision decimalsign Peter Otten <__peter__@web.de> - 2012-07-20 16:31 +0200
Re: logging time format millisecond precision decimalsign David Bolen <db3l.net@gmail.com> - 2012-07-20 14:50 -0400
| From | "Alex van der Spek" <zdoor@xs4all.nl> |
|---|---|
| Date | 2012-07-20 15:00 +0200 |
| Subject | logging time format millisecond precision decimalsign |
| Message-ID | <50095684$0$6957$e4fe514c@news2.news.xs4all.nl> |
I use this formatter in logging: formatter = logging.Formatter(fmt='%(asctime)s \t %(name)s \t %(levelname)s \t %(message)s') Sample output: 2012-07-19 21:34:58,382 root INFO Removed - C:\Users\ZDoor\Documents The time stamp has millisecond precision but the decimal separator is a comma. Can I change the comma (,) into a period (.) and if so how? Thanks in advance, Alex van der Spek
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-07-20 16:31 +0200 |
| Message-ID | <mailman.2346.1342794732.4697.python-list@python.org> |
| In reply to | #25692 |
Alex van der Spek wrote:
> I use this formatter in logging:
>
> formatter = logging.Formatter(fmt='%(asctime)s \t %(name)s \t
> %(levelname)s \t %(message)s')
>
> Sample output:
>
> 2012-07-19 21:34:58,382 root INFO Removed - C:\Users\ZDoor\Documents
>
> The time stamp has millisecond precision but the decimal separator is a
> comma.
>
> Can I change the comma (,) into a period (.) and if so how?
I think you have to subclass Formatter.formatTime(). Here's a monkey-
patching session to get you started:
>>> import logging
>>> logging.basicConfig(format="%(asctime)s")
>>> logging.getLogger().error("foo")
2012-07-20 16:17:39,364
>>> _formatTime = logging.Formatter.formatTime
>>> def formatTime(*args):
... return _formatTime(*args).replace(",", ".")
...
>>> logging.Formatter.formatTime = formatTime
>>> logging.getLogger().error("foo")
2012-07-20 16:20:20.838
See also
http://docs.python.org/dev/py3k/library/logging.html#logging.Formatter.formatTime
"""
Changed in version 3.3: Previously, the default ISO 8601 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).
"""
[toc] | [prev] | [next] | [standalone]
| From | David Bolen <db3l.net@gmail.com> |
|---|---|
| Date | 2012-07-20 14:50 -0400 |
| Message-ID | <m2fw8m6zd7.fsf@valheru.db3l.homeip.net> |
| In reply to | #25692 |
"Alex van der Spek" <zdoor@xs4all.nl> writes:
> I use this formatter in logging:
>
> formatter = logging.Formatter(fmt='%(asctime)s \t %(name)s \t %(levelname)s
> \t %(message)s')
>
> Sample output:
>
> 2012-07-19 21:34:58,382 root INFO Removed - C:\Users\ZDoor\Documents
>
> The time stamp has millisecond precision but the decimal separator is a
> comma.
>
> Can I change the comma (,) into a period (.) and if so how?
I do it by:
1. Replacing the default date format string to exclude ms.
2. Including %(msecs)03d in the format string where appropriate. Using 'd'
instead of s truncates rather than shows the full float value.
So in your case, I believe that changing your formatter creation to:
formatter = logging.Formatter(fmt='%(asctime)s.%(msecs)03d \t %(name)s \t %(levelname)s \t %(message)s', '%Y-%m-%d %H:%M:%S')
should work. This uses the same date format as the default, but
without ms, though of course you could also opt to make any other date
format you prefer.
-- David
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web