日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python logging 毫秒级别的时间打印

發(fā)布時間:2025/4/5 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python logging 毫秒级别的时间打印 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

需要查看代碼性能,所以需要毫秒級別的打印。但是,在logging包中,查了源碼之后確認(rèn)使用的是time包的datefmt。而查看Python的doc文檔,在time包中并沒有提供毫秒的datefmt,所以,只能重寫logging中取時間的函數(shù)。如下:


class Formatter(logging.Formatter):def formatTime(self, record, datefmt=None):"""Return the creation time of the specified LogRecord as formatted text.This method should be called from format() by a formatter whichwants to make use of a formatted time. This method can be overriddenin formatters to provide for any specific requirement, but thebasic behaviour is as follows: if datefmt (a string) is specified,it is used with time.strftime() to format the creation time of therecord. Otherwise, the ISO8601 format is used. The resultingstring is returned. This function uses a user-configurable functionto convert the creation time to a tuple. 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 astime.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."""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)s = str(datetime.datetime.now())return s


此處,在datefmt為None的時候,使用datetime包中的時間替代。

之后,只要在實(shí)例化Formater的時候使用該類,并設(shè)置datefmt為None即可。

?formatter = Formatter(fmt, datefmt=None)



總結(jié)

以上是生活随笔為你收集整理的python logging 毫秒级别的时间打印的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。