Skip to content

Instantly share code, notes, and snippets.

@barend
Created October 26, 2020 09:44
Show Gist options
  • Save barend/7b09dfc95c68083402964ff11c54c347 to your computer and use it in GitHub Desktop.
Save barend/7b09dfc95c68083402964ff11c54c347 to your computer and use it in GitHub Desktop.
Configure Python logging with ISO-8601 format and UTC time
import logging
from time import gmtime
# https://stackoverflow.com/a/7517430/49489
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s.%(msecs)03dZ [%(name)s] %(message)s",
datefmt="%Y-%m-%dT%H:%M:%S",
)
logging.Formatter.converter = gmtime
@kirillsulim
Copy link

This does work only if your server time is set to UTC. It is just add Z at your local time

@Arnold1
Copy link

Arnold1 commented Feb 26, 2023

what to change if you need EST time?

@kentyman23
Copy link

This does work only if your server time is set to UTC. It is just add Z at your local time

Actually, this last line ensures the time it uses is UTC:

logging.Formatter.converter = gmtime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment