Skip to content
Browse files

edit

  • Loading branch information...
1 parent c4759d4 commit ce39ecee28bae4ffaa282a856ff2ba45764ce952 @ohsc ohsc committed with anandology Jan 20, 2010
Showing with 40 additions and 0 deletions.
  1. +40 −0 cookbook/logging.zh-cn.md
View
40 cookbook/logging.zh-cn.md
@@ -0,0 +1,40 @@
+---
+layout: default
+title: 管理自带webserver日志
+---
+
+# 管理自带webserver日志
+
+## 问题
+
+如何操作web.py自带的webserver的日志?
+
+## 解法
+
+我们可以用[wsgilog](http://pypi.python.org/pypi/wsgilog/)来操作内置的webserver的日志,并做其为中间件加到应用中。
+
+如下,写一个Log类继承wsgilog.WsgiLog,在_init_中把参数传给基类,如[这个例子](http://github.com/harryf/urldammit/blob/234bcaae6deb65240e64ee3199213712ed62883a/dammit/log.py):
+
+ 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)

0 comments on commit ce39ece

Please sign in to comment.
Something went wrong with that request. Please try again.