I'm using logging for my project and the format is
'format': '%(asctime)s %(message)s'
however, the time didn't equal to my local time. e.g
2013-01-08 11:04:07,383 message (this is UTC-6 time)
I want to know
I have fixed it , the problem is that I need to set the time zone in setting.py !!!
From the docs:
class logging.Formatter(fmt=None, datefmt=None)
Returns a new instance of the Formatter class. The instance is initialized with a format string for the message as a whole, as well as a format string for the date/time portion of a message. If no fmt is specified, '%(message)s' is used. If no datefmt is specified, the ISO8601 date format is used.
Also:
If the formatting string contains
'(asctime)'
,formatTime()
is called to format the event time.
So, you can either specify a datefmt
, or create a custom Formatter
subclass with your own formatTime
override.
And, if you want to choose between local time and GMT time (or anything else):
By default, time.localtime() is used; to change this for a particular formatter instance, set the converter attribute to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the converter attribute in the Formatter class.
The format
and datefmt
arguments can also be passed to basicConfig
, if you're not using an explicit Formatter
. You can't set a converter
this way—but you don't have to, because the default is local time.
So:
logging.basicConfig(format='%(asctime)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
log = logging.getLogger(__name__)
log.error('Out of cheese!')
I ran this at 16:11:30 local time, and the output is:
2013-01-08 16:11:30 Out of cheese!
In fact, it looks like all you're trying to do is knock the milliseconds off the standard time format, which you can do even more simply by just truncating the string:
logging.basicConfig(format='%(asctime)-.19s %(message)s')
However, I think it makes more sense to specify the date format explicitly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With