相关疑难解决方法(0)

在 aiohttp 2 中指定日志请求格式

我正在将 aiohttp 2 与 Python 3.6 一起使用,并希望记录进入应用程序的请求。

我做了:

# 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)
Run Code Online (Sandbox Code Playgroud)

现在,当应用程序运行时,我会得到以下格式的日志:

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" 
Run Code Online (Sandbox Code Playgroud)

message组件有一个冗余时间戳,我想自定义其格式。该文件说,它应该是可能的,但我不明白如何使它实际工作,也没有代码示例。

我只发现了这种用法,但有:

mylogger = logging.Logger('aiohttp.access')
mylogger.setLevel(logging.DEBUG)
mylogger.addHandler(ch)

handler = …
Run Code Online (Sandbox Code Playgroud)

python logging python-3.x aiohttp

2
推荐指数
1
解决办法
975
查看次数

标签 统计

aiohttp ×1

logging ×1

python ×1

python-3.x ×1