Django: keep UTC time on registration when TIME_ZONE settings change

I have set the timezone in settings.py

my django project file :

TIME_ZONE = 'US/Eastern'

and now my magazines contain US / Eastern.

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

+3


source to share


1 answer


Django uses Python loggers, so there shouldn't be anything Django-specific here.

According to the logging documentation, the setup logging.Formatter.converter = time.gmtime

should do all logs in UTC.

Alternatively, you can create your own class Formatter

to use UTC:



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

      

And then configure it using the key ()

(registered here ) in the dictconfig file:

LOGGING = {
    'formatters': {
        'utc': { 
            '()': 'my.package.UtcFormatter',
            'format': '...',
            'datefmt': '...',
        }
    }
}

      

+7


source







All Articles