1

There are questions related on using milliseconds in python logging, there are questions related on how to use UTC, but I cannot see how to combine these two. Based on this question, how can I use the UTC time for logging instead the local time?

def formatTime(self, record, datefmt=None):
    ct = self.converter(record.created)
    if datefmt:
        s = time.strftime(datefmt, ct)
    else:
        t = time.strftime("%Y-%m-%d %H:%M:%S", ct)
        s = "%s,%03d" % (t, record.msecs)
    return s
Community
  • 1
  • 1
Alex
  • 41,580
  • 88
  • 260
  • 469

1 Answers1

0

If you use datetime.datetime.utcfromtimestamp as your converter function, it'll take the local timestamp to a datetime that, as it has a strftime() method, will work just fine here.

(Unlike time.gmtime which returns a struct_time that you would have to convert again.)

Jason S
  • 13,538
  • 2
  • 37
  • 42