logging time format millisecond precision decimalsign

Discussion in 'Python' started by Alex van der Spek, Jul 20, 2012.

  1. 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
     
    Alex van der Spek, Jul 20, 2012
    #1
    1. Advertising

  2. Alex van der Spek

    Peter Otten Guest

    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).
    """
     
    Peter Otten, Jul 20, 2012
    #2
    1. Advertising

  3. Alex van der Spek

    David Bolen Guest

    "Alex van der Spek" <> 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
     
    David Bolen, Jul 20, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. palmis

    UTC time in millisecond

    palmis, Jan 10, 2006, in forum: Java
    Replies:
    26
    Views:
    24,867
    Roedy Green
    Jan 19, 2006
  2. WeAreBorg

    timer with millisecond precision

    WeAreBorg, Sep 29, 2005, in forum: C Programming
    Replies:
    2
    Views:
    389
    Skarmander
    Sep 29, 2005
  3. etantonio
    Replies:
    1
    Views:
    426
    John B. Matthews
    Mar 16, 2009
  4. Warren

    Millisecond in time.

    Warren, Jul 1, 2003, in forum: Ruby
    Replies:
    11
    Views:
    287
    Harry Ohlsen
    Jul 2, 2003
  5. Alex van der Spek

    logging time format millisecond precision

    Alex van der Spek, Jul 19, 2012, in forum: Python
    Replies:
    0
    Views:
    180
    Alex van der Spek
    Jul 19, 2012
Loading...

Share This Page