Пример #1
0
    def __init__(self, handlers, **settings):
        web.Application.__init__(self, handlers, **settings)
        #
        # 系统记录的日志缓存
        #
        self.log_caches = []
        self.log_size = 100

        #
        # 缓存系统
        #
        self.pub_sub = PubSubManager(self)
        self.ip_auth = False
        self.trust_ips = ["127.0.0.1"]
        self.enable_piwik = False
Пример #2
0
class SEApplication(web.Application):
    """
    @summary: Mysql数据连接应用管理模块
    """

    def __init__(self, handlers, **settings):
        web.Application.__init__(self, handlers, **settings)
        #
        # 系统记录的日志缓存
        #
        self.log_caches = []
        self.log_size = 100

        #
        # 缓存系统
        #
        self.pub_sub = PubSubManager(self)
        self.ip_auth = False
        self.trust_ips = ["127.0.0.1"]
        self.enable_piwik = False

    def init(self):
        """
        @summary: 初始化所有部分

        @return: 初始化成功返回
        """
        #
        # 初始化处理句柄
        #
        ret = self._init_handlers()
        if not ret:
            return ret

        #
        # 初始化redis监控句柄
        #
        self.pub_sub.init()

        #
        # 初始化piwik统计模块
        #
        self._init_piwik()
        
        if not config.has_section("system"):
            return ret

        #
        # 添加ip检测开关量
        #
        if config.has_option("system", "enable_ip_auth"):
            enable_ip_auth = config.get("system", "trust_ips", "false")
            if enable_ip_auth == "true":
                self.ip_auth = True

        if not config.has_option("system", "trust_ips"):
            return ret

        trust_ips = config.get("system", "trust_ips", "0")
        trust_ip_list = trust_ips.split('|')
        for ip in trust_ip_list:
            if ip in self.trust_ips:
                continue
            self.trust_ips.append(ip)
        return ret

    def _init_handlers(self):
        """
        @summary: 加载应用程序模块

        @return: 初始化成功返回True,否则返回False
        """
        handlers = []
        if config.has_section("services"):
            if config.has_option("services", "enable_pub") and \
                            config.get("services", "enable_pub", "0") == "1":
                handlers.append(((r"/sub/(.+)"), SubHandler))
                handlers.append(((r"/pub/(.+)"), PubHandler))
                
                #
                # 批量订阅支持
                #
                handlers.append(((r"/subs"), SubsHandler))
                handlers.append(((r"/pubs"), PubsHandler))

        handlers.append(((r"/logs"), LogHandler))
        handlers.append(((r"/watch"), WatchHandler))
        host_pattern = ".*$"
        if len(handlers) <= 0:
            return False

        self.add_handlers(host_pattern, handlers)
        return True

    def _init_piwik(self):
        """
        @summary: 初始化统计模块piwik

        @return: 初始化成功返回True,否则返回False
        """
        if not config.has_section("piwik") or \
            not config.has_option("piwik", "enable"):
            return False

        enable_piwik = config.get("piwik", "enable", "false")
        if enable_piwik != "true":
            return True

        self.enable_piwik = True
        return True

    def log_request(self, handler):
        """
        @summary: 重构每个HTTP请求统计模块,进行计数统计并在前端进行显示

        By default writes to the python root logger.  To change
        this behavior either subclass Application and override this method,
        or pass a function in the application settings dictionary as
        ``log_function``.
        """
        if "log_function" in self.settings:
            self.settings["log_function"](handler)
            return

        #
        # 解析处理服务器的
        #
        if handler.request.method == "POST" and handler.request.uri.lower() == "/logs":
            return

        if handler.get_status() < 400:
            log_method = logger.info
        elif handler.get_status() < 500:
            log_method = logger.warning
        else:
            log_method = logger.error
        request_time = 1000.0 * handler.request.request_time()
        log_msg = "%d %s %.2fms" % ( handler.get_status(),
                   handler._request_summary(), request_time)
        log_method(log_msg)

        log_item = {
            "id": str(uuid.uuid4()),
            "time": self.format_time(handler.request._start_time),
            'uri' : handler.request.uri,
            'method': handler.request.method,
            'status' : handler.get_status(),
            'RequestTime': "%.2f" % request_time,
            'UserAgent': handler.request.headers.get("User-Agent", ""),
            "body": handler.request.host + " " + handler._request_summary(),
        }

        #
        # 时间 url
        #  2013-07-18 17:40:25.903 /tasks/domain_query 200 203ms 0kb AppEngine-Google; (+http://code.google.com/appengine)
        # to_basestring is necessary for Python 3's json encoder,
        # which doesn't accept byte strings.
        #
        log_item["html"] = tornado.escape.to_basestring(
            handler.render_string("log/log.html", log=log_item))

        #
        # 缓存到内部系统中
        #
        self.log_caches.append(log_item)
        if len(self.log_caches) > self.log_size:
            self.log_caches = self.log_caches[-self.log_size:]

        sig_logs.send(new_log=[log_item])

    def format_time(self, value):
        """
        @summary 获取开始执行时间
        """
        t_value = time.localtime(value)
        t = time.strftime("%Y-%m-%d %H:%M:%S", t_value)
        s = "%s.%03d" % (t, int(round(value * 1000)) % 1000)
        return s