1
我在Python 3.6中使用aiohttp 2並希望記錄到達應用程序的請求。在aiohttp中指定日誌請求格式2
我所做的:
# use ISO timestamps
from time import gmtime
logging.Formatter.converter = gmtime
# create a formatter
ch = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s %(levelname)s %(name)s - %(message)s', '%Y-%m-%dT%H:%M:%S')
ch.setFormatter(formatter)
# show all emssages (default is WARNING)
logging.getLogger('aiohttp.access').setLevel(logging.DEBUG)
# attach the handler
logging.getLogger('aiohttp.access').addHandler(ch)
,現在當應用程序運行時,我得到一個日誌的格式如下:
2017-04-19T16:02:17 INFO aiohttp.access - 127.0.0.1 - - [19/Apr/2017:16:02:17 +0000] "GET /test HTTP/1.1" 404 547 "-" "curl/7.51.0"
的message
組件具有冗餘的時間戳,我想自定義其格式。 documentation說它應該是可能的,但我不明白如何使它實際工作,並沒有代碼示例。
我發現只有this usage但:
mylogger = logging.Logger('aiohttp.access')
mylogger.setLevel(logging.DEBUG)
mylogger.addHandler(ch)
handler = app.make_handler(
logger=mylogger,
access_log_format='%r %s %b',
)
應用不產生日誌可言。我不明白make_handler
究竟做了什麼,而previous question沒有幫助。
如何格式化message
部分日誌並插入aiohttp文檔中列出的元素?
你叫'aiohttp.web.Application.make_handler()'? – 2017-04-19 16:18:44
不,在這個例子中,他們在應用程序實例上調用它。我試過了,它說'make_handler()缺少1個需要的位置參數:'self''所以它看起來是一個實例方法 – Jacopofar