Exemplo n.º 1
0
    def make_http_server(self):
        multiprocessing = False
        if settings['PROCESSES'] and settings['PROCESSES'] > 1:
            if settings['DEBUG']:
                app_log.info('Multiprocess could not be used in debug mode')
            else:
                multiprocessing = True

        if self.io_loop:
            if not self.io_loop.initialized():
                # this means self.io_loop is a customized io_loop, so `install` should be called
                # to make it the singleton instance
                #print self.io_loop.__class__.__name__
                self.io_loop.install()
        else:
            # NOTE To support running tornado for multiple processes, we do not instance ioloop if multiprocessing is True
            if not multiprocessing:
                self.io_loop = IOLoop.instance()

        http_server_options = self.get_httpserver_options()
        http_server = HTTPServer(self.application, io_loop=self.io_loop, **http_server_options)
        listen_kwargs = {}
        if settings.get('ADDRESS'):
            listen_kwargs['address'] = settings.get('ADDRESS')

        if multiprocessing:
            # Multiprocessing mode
            try:
                http_server.bind(settings['PORT'], **listen_kwargs)
            except socket.error, e:
                app_log.warning('socket.error detected on http_server.listen, set ADDRESS="0.0.0.0" in settings to avoid this problem')
                raise e
            http_server.start(settings['PROCESSES'])
Exemplo n.º 2
0
def log_response(handler):
    """
    Acturally, logging response is not a server's responsibility,
    you should use http tools like Chrome Developer Tools to analyse the response.

    Although this function and its setting(LOG_RESPONSE) is not recommended to use,
    if you are laze as I was and working in development, nothing could stop you.
    """
    content_type = handler._headers.get('Content-Type', None)
    headers_str = handler._generate_headers()
    block = 'Response Infomations:\n' + headers_str.strip()

    if content_type and ('text' in content_type or 'json' in content_type):
        limit = 0
        if 'LOG_RESPONSE_LINE_LIMIT' in settings:
            limit = settings['LOG_RESPONSE_LINE_LIMIT']

        def cut(s):
            if limit and len(s) > limit:
                return [s[:limit]] + cut(s[limit:])
            else:
                return [s]

        body = ''.join(handler._write_buffer)
        lines = []
        for i in body.split('\n'):
            lines += ['| ' + j for j in cut(i)]
        block += '\nBody:\n' + '\n'.join(lines)
    app_log.info(block)
Exemplo n.º 3
0
Arquivo: base.py Projeto: reorx/torext
def log_response(handler):
    """
    Acturally, logging response is not a server's responsibility,
    you should use http tools like Chrome Developer Tools to analyse the response.

    Although this function and its setting(LOG_RESPONSE) is not recommended to use,
    if you are laze as I was and working in development, nothing could stop you.
    """
    content_type = handler._headers.get('Content-Type', None)
    headers_str = handler._generate_headers()
    block = 'Response Infomations:\n' + headers_str.strip()

    if content_type and ('text' in content_type or 'json' in content_type):
        limit = 0
        if 'LOG_RESPONSE_LINE_LIMIT' in settings:
            limit = settings['LOG_RESPONSE_LINE_LIMIT']

        def cut(s):
            if limit and len(s) > limit:
                return [s[:limit]] + cut(s[limit:])
            else:
                return [s]

        body = ''.join(handler._write_buffer)
        lines = []
        for i in body.split('\n'):
            lines += ['| ' + j for j in cut(i)]
        block += '\nBody:\n' + '\n'.join(lines)
    app_log.info(block)
Exemplo n.º 4
0
Arquivo: app.py Projeto: xoxoj/torext
    def log_app_info(self):
        mode = settings['DEBUG'] and 'Debug' or 'Product'
        content = '\nMode %s, Service Info:' % mode
        loggers_info = {}
        for k in settings['LOGGERS']:
            _logger = logging.getLogger(k)
            #loggers_info[k] = {i: getattr(_logger, i) for i in ('level', 'handlers', 'propagate')}
            loggers_info[k] = dict((i, getattr(_logger, i)) for i in ('level', 'handlers', 'propagate'))
            level = loggers_info[k]['level']
            loggers_info[k]['level'] = '%s (%s)' % (level, logging._levelNames[level])

        info = {
            'Project': settings['PROJECT'] or 'None (better be assigned)',
            'Port': settings['PORT'],
            'Processes': settings['DEBUG'] and 1 or settings['PROCESSES'],
            'Loggers': loggers_info,
            'Locale': settings['LOCALE'],
            'Debug': settings['DEBUG'],
            'Home': 'http://127.0.0.1:%s' % settings['PORT'],
        }

        #if settings['DEBUG']:
        buf = []
        for host, rules in self.application.handlers:
            buf.append(host.pattern)
            for i in rules:
                buf.append('  ' + i.regex.pattern)
        info['URL Patterns(by sequence)'] = '\n    ' + '\n    '.join(buf)

        for k in ['Project', 'Port', 'Processes',
                  'Loggers', 'Locale', 'Debug', 'Home', 'URL Patterns(by sequence)']:
            content += '\n- %s: %s' % (k, info[k])

        app_log.info(content)
Exemplo n.º 5
0
Arquivo: app.py Projeto: xoxoj/torext
    def _init_infrastructures(self):
        multiprocessing = False
        if settings['PROCESSES'] and settings['PROCESSES'] > 1:
            if settings['DEBUG']:
                app_log.info('Multiprocess could not be used in debug mode')
            else:
                multiprocessing = True

        if self.io_loop:
            if not self.io_loop.initialized():
                # this means self.io_loop is a customized io_loop, so `install` should be called
                # to make it the singleton instance
                #print self.io_loop.__class__.__name__
                self.io_loop.install()
        else:
            # NOTE To support running tornado for multiple processes, we do not instance ioloop if multiprocessing is True
            if not multiprocessing:
                self.io_loop = IOLoop.instance()

        application = self._make_application()

        http_server_options = self.get_httpserver_options()
        http_server = HTTPServer(application, io_loop=self.io_loop, **http_server_options)
        listen_kwargs = {}
        if settings.get('ADDRESS'):
            listen_kwargs['address'] = settings.get('ADDRESS')

        if multiprocessing:
            # Multiprocessing mode
            try:
                http_server.bind(settings['PORT'], **listen_kwargs)
            except socket.error, e:
                app_log.warning('socket.error detected on http_server.listen, set ADDRESS="0.0.0.0" in settings to avoid this problem')
                raise e
            http_server.start(settings['PROCESSES'])
Exemplo n.º 6
0
Arquivo: app.py Projeto: reorx/torext
    def log_app_info(self, application=None):
        current_settings = self.settings

        # Log settings
        mode = current_settings['DEBUG'] and 'Debug' or 'Product'
        content = '\nMode %s, Service Info:' % mode
        loggers_info = {}
        for k in current_settings['LOGGERS']:
            _logger = logging.getLogger(k)
            # loggers_info[k] = {i: getattr(_logger, i) for i in ('level', 'handlers', 'propagate')}
            loggers_info[k] = dict((i, getattr(_logger, i))
                                   for i in ('level', 'handlers', 'propagate'))
            level = loggers_info[k]['level']
            loggers_info[k]['level'] = '%s (%s)' % (level,
                                                    logging._levelNames[level])

        info = {
            'Project':
            current_settings['PROJECT'] or 'None',
            'Port':
            current_settings['PORT'],
            'Processes':
            current_settings['DEBUG'] and 1 or current_settings['PROCESSES'],
            'Loggers':
            loggers_info,
            'Locale':
            current_settings['LOCALE'],
            'Debug':
            current_settings['DEBUG'],
            'Home':
            'http://%s:%s' %
            (current_settings['ADDRESS'], current_settings['PORT']),
        }

        # Log urls
        if not application:
            application = self.application

        # if settings['DEBUG']:
        buf = []
        for host, rules in application.handlers:
            buf.append(host.pattern)
            for i in rules:
                buf.append('  ' + i.regex.pattern)
        info['URL Patterns(by sequence)'] = '\n    ' + '\n    '.join(buf)

        for k in [
                'Project', 'Port', 'Processes', 'Loggers', 'Locale', 'Debug',
                'Home', 'URL Patterns(by sequence)'
        ]:
            content += '\n- %s: %s' % (k, info[k])

        app_log.info(content)
Exemplo n.º 7
0
Arquivo: base.py Projeto: reorx/torext
def log_request(handler):
    """
    Logging request is opposite to response, sometime its necessary,
    feel free to enable it.
    """
    block = 'Request Infomations:\n' + _format_headers_log(handler.request.headers)

    if handler.request.arguments:
        block += '+----Arguments----+\n'
        for k, v in handler.request.arguments.items():
            block += '| {0:<15} | {1:<15} \n'.format(repr(k), repr(v))

    app_log.info(block)
Exemplo n.º 8
0
def log_request(handler):
    """
    Logging request is opposite to response, sometime its necessary,
    feel free to enable it.
    """
    block = 'Request Infomations:\n' + _format_headers_log(
        handler.request.headers)

    if handler.request.arguments:
        block += '+----Arguments----+\n'
        for k, v in handler.request.arguments.items():
            block += '| {0:<15} | {1:<15} \n'.format(repr(k), repr(v))

    app_log.info(block)
Exemplo n.º 9
0
Arquivo: app.py Projeto: reorx/torext
    def setup(self):
        """This function will be called both before `run` and testing started.
        """
        testing = settings.get('TESTING')

        if testing:
            # Fix nose handler in testing situation.
            config = settings['LOGGERS'].get('', {})
            set_nose_formatter(config)
            #print('testing, set nose formatter: {}'.format(config))

        # reset timezone
        os.environ['TZ'] = settings['TIME_ZONE']
        time.tzset()

        # determine project name
        if settings._module:
            project = os.path.split(self.root_path)[1]
            if settings['PROJECT']:
                assert settings['PROJECT'] == project, 'PROJECT specialized in settings (%s) '\
                    'should be the same as project directory name (%s)' % (settings['PROJECT'], project)
            else:
                settings['PROJECT'] = project

        # PROJECT should be importable as a python module
        if settings['PROJECT']:
            # add upper directory path to sys.path if not in
            if settings._module:
                _abs = os.path.abspath
                parent_path = os.path.dirname(self.root_path)
                if not _abs(parent_path) in [_abs(i) for i in sys.path]:
                    sys.path.insert(0, parent_path)
                    app_log.info('Add %s to sys.path' % _abs(parent_path))
            try:
                __import__(settings['PROJECT'])
                app_log.debug('import package `%s` success' %
                              settings['PROJECT'])
            except ImportError:
                raise ImportError(
                    'PROJECT could not be imported, may be app.py is outside the project'
                    'or there is no __init__ in the package.')

        self.is_setuped = True
Exemplo n.º 10
0
Arquivo: app.py Projeto: xoxoj/torext
    def setup(self):
        """This function will be called both before `run` and testing started.
        """
        testing = settings.get('TESTING')

        if testing:
            # Fix nose handler in testing situation.
            config = settings['LOGGERS'].get('', {})
            set_nose_formatter(config)
            print 'testing, set nose formatter: %s' % config

        # reset timezone
        os.environ['TZ'] = settings['TIME_ZONE']
        time.tzset()

        # determine project name
        if settings._module:
            project = os.path.split(self.root_path)[1]
            if settings['PROJECT']:
                assert settings['PROJECT'] == project, 'PROJECT specialized in settings (%s) '\
                    'should be the same as project directory name (%s)' % (settings['PROJECT'], project)
            else:
                settings['PROJECT'] = project

        # PROJECT should be importable as a python module
        if settings['PROJECT']:
            # add upper directory path to sys.path if not in
            if settings._module:
                _abs = os.path.abspath
                parent_path = os.path.dirname(self.root_path)
                if not _abs(parent_path) in [_abs(i) for i in sys.path]:
                    sys.path.insert(0, parent_path)
                    app_log.info('Add %s to sys.path' % _abs(parent_path))
            try:
                __import__(settings['PROJECT'])
                app_log.debug('import package `%s` success' % settings['PROJECT'])
            except ImportError:
                raise ImportError('PROJECT could not be imported, may be app.py is outside the project'
                                  'or there is no __init__ in the package.')

        self.is_setuped = True