Commits

Anonymous committed 09e58a4

Add logging

  • Participants
  • Parent commits 11ad2d7

Comments (0)

Files changed (6)

 glob:*.pyc
 glob:.pydevproject
 glob:.project
+glob:logs/*.log
                 userPassword = base64.b64decode(userPassword)
                 user, password = userPassword.split(':', 1)
                 if self.authenFunc(user, password):
+                    self.log.info('Login as %s from %s', user, req.remote_addr)
                     self.sessionTable.login(req.remote_addr)
                     return self.app(environ, start_response)
                 else:
+[loggers]
+keys=root,http
+
+[handlers]
+keys=consoleHandler,fileHandler,httpFileHandler,errorFileHandler
+
+[formatters]
+keys=consoleFormatter,httpFormatter
+
+#
+# loggers
+#
+[logger_root]
+level=INFO
+handlers=consoleHandler,fileHandler,errorFileHandler
+
+[logger_http]
+level=INFO
+handlers=httpFileHandler
+qualname=http
+propagate=0
+
+#
+# handlers
+#
+[handler_consoleHandler]
+class=StreamHandler
+level=NOTSET
+formatter=consoleFormatter
+args=(sys.stdout,)
+
+[handler_fileHandler]
+class=handlers.RotatingFileHandler
+level=NOTSET
+formatter=consoleFormatter
+args=('logs/middleman.log', 'a', 1*1024*1024, 50, 'utf8')
+
+[handler_httpFileHandler]
+class=handlers.RotatingFileHandler
+level=NOTSET
+formatter=httpFormatter
+args=('logs/http.log', 'a', 1*1024*1024, 50, 'utf8')
+
+[handler_errorFileHandler]
+class=FileHandler
+level=ERROR
+formatter=consoleFormatter
+args=('logs/errors.log',)
+
+#
+# formatters
+#
+[formatter_consoleFormatter]
+format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
+
+[formatter_httpFormatter]
+format=%(message)s

File logs/.empty

Empty file added.
 from gevent import monkey; monkey.patch_all()
 
 import logging
+import logging.config
 
 import yaml
-from gevent import pywsgi
 
 import proxy
 import authen
+import wsgi
 
 def authenticate(url, sharedSecret, username, password):
     import time
     interface = serverConfig['interface']
     port = serverConfig['port']
     print "Serving on %s:%d..." % (interface, port)
-    server = pywsgi.WSGIServer(
+    server = wsgi.WSGIServer(
         (interface, port), 
         app, 
         handler_class=proxy.WSGIHandler
     server.serve_forever()
 
 if __name__ == '__main__':
-    logging.basicConfig(level=logging.INFO)
+    logging.config.fileConfig('logging.cfg')
     import sys
     if len(sys.argv) > 1:
         configPath = sys.argv[1]
+'''
+Created on 2010/7/19
+
+@author: Victor-mortal
+'''
+import os
+import logging
+
+from gevent import pywsgi
+
+class LoggerFile(object):
+    def __init__(self, logger, level=logging.INFO):
+        self.logger = logger
+        self.level = level
+        
+    def write(self, data):
+        self.logger.log(self.level, data.strip())
+
+class WSGIServer(pywsgi.WSGIServer):
+    
+    def __init__(
+        self,
+        listener,
+        application,
+        handler=None,
+        httpLogger=None,
+        logger=None,
+        backlog=None, 
+        spawn='default', 
+        handler_class=None,
+        environ=None,
+        **ssl_args
+    ):
+        self.httpLogger = httpLogger
+        if self.httpLogger is None:
+            self.httpLogger = logging.getLogger('http')
+        self.logger = logger
+        if self.logger is None:
+            self.logger = logging.getLogger('wsgi')
+        self.logFile = LoggerFile(self.httpLogger)
+        self.handler = handler
+        
+        pywsgi.WSGIServer.__init__(self, 
+            listener, application, backlog, spawn, self.logFile, handler_class, 
+            environ, **ssl_args)
+        
+    def serve_forever(self, stop_timeout=None):
+        pywsgi.WSGIServer.serve_forever(self, stop_timeout)
+        self.logger.info('Server terminated')
+        
+    def defaultHandler(self, socket, address):
+        return pywsgi.WSGIServer.handle(self, socket, address)
+        
+    def handle(self, socket, address):
+        """Check the request and decide whether to handle
+        
+        """
+        if self.handler is None:
+            return self.defaultHandler(socket, address)
+        return self.handler(self.defaultHandler, socket, address)