Is it possible and how to set the logging timezone to GMT?
(i.e. the %(asctime)s
parameter in the format)
Is it possible and how to set the logging timezone to GMT?
(i.e. the %(asctime)s
parameter in the format)
logging.Formatter.converter = time.gmtime
(documented in the docstring of logging.Formatter.formatTime
)
'()' : 'my.package.customFormatterFactory',
We cannot set the 'class': 'my.package.customFormatterFactory'
for a formatter. –
Fajardo logging.Formatter.converter = time.gmtime
will work for any formatter that inherits from the logging.Formatter
class. Just check whether Django formatter is inheriting from Python's main Formatter class, which I bet it does. –
Scalenus From the python 3 docs:
import time
class UTCFormatter(logging.Formatter):
converter = time.gmtime
Just setting logging.Formatter.converter = time.gmtime
is ineffective for me in Python 2.5.
So I created a child class with it set, and use that in place of logging.Formatter:
class UTCFormatter(logging.Formatter):
converter = time.gmtime
__init__()
method, as my first edit suggested.) –
Julenejulep Here is code example:
import logging, logging.handlers
import time
logit = logging.getLogger('logit')
handler = logging.handlers.RotatingFileHandler("file.log", maxBytes=20000, backupCount=5)
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s %(levelname)8s: %(message)s')
handler.setFormatter(formatter)
logging.Formatter.converter = time.gmtime
logit.addHandler(handler)
logit.info("test log message")
Output:
2019-11-14 16:34:22,967 INFO: test log message
I had problems getting the popular answers from this post and similar posts related to using time.gmtime with the logging converter. I stumbled across this helpful post about logging timestamps in iso8601 which sent me in the right direction to eventually solve my issue.
This solution works in Python 3.10.8. It involved setting the converter = time.gmtime
and overriding the formatTime
. This may be unnecessary in future releases of Python and there may be an easier way to do it as well. Note, setting converter = time.gmtime()
will not work and results in an error.
import logging
import time
class UtcIsoFormatter(logging.Formatter):
"""Custom logging.Formatter class for logging UTC time in ISO8601 format
"""
# Use gmtime or universal coordinated time or utc
converter = time.gmtime # must not include '()'
# Format the time for this log event
def formatTime(self, record, datefmt=None):
ct = self.converter(record.created)
if datefmt:
s = time.strftime(datefmt, ct)
else:
# Default UTC ISO8601 format of "YYYY-mm-ddTHH:MM:SS.fffZ"
t = time.strftime("%Y-%m-%dT%H:%M:%S", ct)
s = f"{t}.{record.msecs:03.0f}Z"
return s
logger = logging.getLogger(__name__)
logger.setLevel(level="DEBUG")
formatter = UtcIsoFormatter(fmt="%(asctime)s - %(levelname)s - %(message)s")
logHandlerConsole = logging.StreamHandler()
logHandlerConsole.setFormatter(formatter)
logger.addHandler(logHandlerConsole)
logger.debug("Testing")
This works:
os.environ['TZ'] = 'UTC'
time.tzset()
But does change the TZ environment for the whole process and any sub-processes. Not necessarily a bad thing, but something to be aware of before doing this.
Alternately if you set the TZ environment variable from outside (whether just in whatever runs the python, or at a whole system level) before you run python, that also works.
I've had problems with both of these answers. So I just changed global timezone for the whole script:
os.environ['TZ'] = 'Europe/London'
time.tzset()
© 2022 - 2024 — McMap. All rights reserved.