22

Is there a way to make the Python logger output dates in ISO8601 format?

My logger is set up like this...

logging.basicConfig(
    format="%(message)s - %(asctime)s)

From the Python docs (located here: https://docs.python.org/2/howto/logging.html) you can see the following:

The default format for date/time display (shown above) is ISO8601. If you need more control over the formatting of the date/time, provide a datefmt argument to basicConfig, as in this example:

The only problem is that the date output is not ISO8601 format. The date output by the above formatter is:

2018-06-15 11:07:41,454

This is not ISO8601 format as defined here: https://en.wikipedia.org/wiki/ISO_8601

What is the easiest way to get the date in the correct format? Can this be done out of the box or do I need to import a package to do it?

I've tried adding a date formatter e.g. datefmt="%Y-%m-%dT%H:%M:%S.%f %Z" but some of the formatting characters were not recognised - namely %f and %Z gave a textual description of the timezone and not a numeric offset.

Remotec
  • 10,304
  • 25
  • 105
  • 147

1 Answers1

28

This works for me in most situations:

logging.basicConfig(
    format="%(asctime)s %(message)s",
    datefmt="%Y-%m-%dT%H:%M:%S%z"
)

The output looks like:

2019-11-09T01:18:13-0800 Something logged here

Update:

A one-liner I've been using to include miliseconds:

logging.Formatter.formatTime = (lambda self, record, datefmt=None: datetime.datetime.fromtimestamp(record.created, datetime.timezone.utc).astimezone().isoformat(sep="T",timespec="milliseconds"))

The output looks like

2021-08-05T22:43:02.985614+00:00 Something logged here

evandrix
  • 6,041
  • 4
  • 27
  • 38
Jack Casey
  • 1,628
  • 11
  • 18
  • 1
    how can the milliseconds portion be added to the timestamp, i.e. "%f"? logging doesn't recognize the %f formatter when I try. – quantif May 13 '20 at 11:26
  • 1
    For the milliseconds, you can fiddle with `datefmt` and `default_msec_format `. But imho it is better to subclass `loggin.Formater`. – Wolfgang Kuehn Jan 20 '21 at 09:49
  • `self` should not be in the lambda function. If it is, then when `.formatTime` is called the value intended for `record` will instead be in `self`. – patyx Jan 09 '23 at 22:25