Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set timestamps on GMT/UTC on Python logging?

Is it possible and how to set the logging timezone to GMT?

(i.e. the %(asctime)s parameter in the format)

like image 283
Jonathan Livni Avatar asked Jun 12 '11 09:06

Jonathan Livni


People also ask

What is Asctime in python logging?

Python time asctime() Method Pythom time method asctime() converts a tuple or struct_time representing a time as returned by gmtime() or localtime() to a 24-character string of the following form: 'Tue Feb 17 23:21:05 2009'.

Is logging Python thread safe?

Although logging module is thread-safe, it's not process-safe. If you want multiple processes to write to the same log file, then you have to manually take care of the access to your file.


2 Answers

logging.Formatter.converter = time.gmtime 

(documented in the docstring of logging.Formatter.formatTime)

like image 74
Sven Marnach Avatar answered Sep 24 '22 16:09

Sven Marnach


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 
like image 44
rakslice Avatar answered Sep 23 '22 16:09

rakslice