问题

如何操作web.py自带的webserver的日志?

解法

我们可以用wsgilog来操作内置的webserver的日志,并做其为中间件加到应用中。

如下,写一个Log类继承wsgilog.WsgiLog,在init中把参数传给基类,如这个例子

import sys, logging
from wsgilog import WsgiLog, LogIO
import config

class Log(WsgiLog):
    def __init__(self, application):
        WsgiLog.__init__(
            self,
            application,
            logformat = '%(message)s',
            tofile = True,
            file = config.log_file,
            interval = config.log_interval,
            backups = config.log_backups
            )
        sys.stdout = LogIO(self.logger, logging.INFO)
        sys.stderr = LogIO(self.logger, logging.ERROR)

接下来,当应用运行时,传递一个引用给上例中的Log类即可(假设上面代码是'mylog'模块的一部分,代码如下):

from mylog import Log
application = web.application(urls, globals())
application.run(Log)

由于博客空间在国外,所以有时会造成不稳定(你懂滴),如果您觉得我的博文对您有帮助, 建议大家多使用RSS访问阅读,本站所有文章均已全文输出。
鲜果阅读器订阅图标

Fork me on GitHub