Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep UTC time in logging while changing the TIME_ZONE settings?

I set the time zone in the settings.py file of my django project:

TIME_ZONE = 'US/Eastern'

and now my logs contain US/Eastern times.

I would like to keep an UTC time in my logs. Is that possible?

like image 947
Michael Avatar asked Oct 18 '14 02:10

Michael


1 Answers

Django uses Python's logging facilities, so there shouldn't be anything Django-specific here.

According to the logging documentation, setting logging.Formatter.converter = time.gmtime should make all logs output in UTC.

Alternatively, you can make your own Formatter class to use UTC:

class UtcFormatter(logging.Formatter): 
    converter = time.gmtime

And then configure it using the () key (documented here) in the dictconfig:

LOGGING = {
    'formatters': {
        'utc': { 
            '()': 'my.package.UtcFormatter',
            'format': '...',
            'datefmt': '...',
        }
    }
}
like image 51
Kevin Christopher Henry Avatar answered Sep 19 '22 21:09

Kevin Christopher Henry