【发布时间】:2013-07-23 22:10:18
【问题描述】:
我正在使用带有cherrypy(提供WSGI)的bottle 用于Web 应用程序。 CherryPy 在此设置中不记录 Web 访问。目前,我几乎使用瓶子的hook plug-in 记录所有内容,如下所示:
import bottle
from bottle import route, static_file, get, post, error, request, template, redirect, response, hook
@hook('after_request')
def log_after_request():
try:
length = response.content_length
except:
try:
length = len(response.body)
except:
length = '???'
print '{ip} - - [{time}] "{method} {uri} {protocol}" {status} {length}'.format(
ip=request.environ.get('REMOTE_ADDR'),
time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
method=request.environ.get('REQUEST_METHOD'),
uri=request.environ.get('REQUEST_URI'),
protocol=request.environ.get('SERVER_PROTOCOL'),
status=response.status_code,
length=length,
)
@route('/index.html')
def index_handler():
return '<h1>Hello, world!</h1>'
app = bottle.default_app()
bottle.run(host='0.0.0.0', port='80', app=app, server='cherrypy', request_queue_size=300, debug=True)
这会向 STDOUT 提供日志条目,如下所示:
192.168.1.1 - - [2013-07-23 17:04:04] "GET /index.html HTTP/1.1" 200 0
这几乎是正确的,除了内容长度始终为 0。瓶子似乎不知道cherrypy返回的内容长度。这是一个正确的评估吗?更重要的是,有没有办法检索它,以便我可以记录它?
我愿意接受更好的方法来使用bottle和cherrypy获取访问日志。
谢谢!
【问题讨论】:
标签: python logging cherrypy bottle content-length