Beispiel #1
0
def log_api(stream, request, query):
    ''' Implements /api/log '''

    # Get logs and options
    logs = LOG.listify()
    options = cgi.parse_qs(query)

    # Reverse logs on request
    if utils.intify(options.get('reversed', ['0'])[0]):
        logs = reversed(logs)

    # Filter according to verbosity
    if utils.intify(options.get('verbosity', ['1'])[0]) < 2:
        logs = [ log for log in logs if log['severity'] != 'DEBUG' ]
    if utils.intify(options.get('verbosity', ['1'])[0]) < 1:
        logs = [ log for log in logs if log['severity'] != 'INFO' ]

    # Human-readable output?
    if utils.intify(options.get('debug', ['0'])[0]):
        logs = [ '%(timestamp)d [%(severity)s]\t%(message)s\r\n' % log
                 for log in logs ]
        body = ''.join(logs).encode('utf-8')
        mimetype = 'text/plain; encoding=utf-8'
    else:
        body = json.dumps(logs)
        mimetype = 'application/json'

    # Compose and send response
    response = Message()
    response.compose(code='200', reason='Ok', body=body, mimetype=mimetype)
    stream.send_response(request, response)
Beispiel #2
0
    def _api_log(self, stream, request, query):

        response = Message()

        dictionary = cgi.parse_qs(query)
        if "debug" in dictionary and utils.intify(dictionary["debug"][0]):
            stringio = StringIO.StringIO()
            for row in LOG.listify():
                ln = "%d [%s]\t%s" % (row["timestamp"], row["severity"],
                                      row["message"])
                stringio.write(ln.encode("utf-8"))
                stringio.write("\r\n")
            stringio.seek(0)
            mimetype = "text/plain"
        else:
            s = json.dumps(LOG.listify())
            stringio = StringIO.StringIO(s)
            mimetype = "application/json"

        response.compose(code="200", reason="Ok", body=stringio,
                         mimetype=mimetype)
        stream.send_response(request, response)
Beispiel #3
0
def log_api(stream, request, query):
    ''' Implements /api/log '''

    #
    # CAVEAT Currently Neubot do not update logs "in real
    # time" using AJAX.  If it did we would run in trouble
    # because each request for /api/log would generate a
    # new access log record.  In turn, a new access log
    # record will cause a new "logwritten" event, leading
    # to a log-caused Comet storm.
    #

    # Get logs and options
    logs = LOG.listify()
    options = cgi.parse_qs(query)

    # Reverse logs on request
    if utils.intify(options.get('reversed', ['0'])[0]):
        logs = reversed(logs)

    # Filter according to verbosity
    if utils.intify(options.get('verbosity', ['1'])[0]) < 2:
        logs = [ log for log in logs if log['severity'] != 'DEBUG' ]
    if utils.intify(options.get('verbosity', ['1'])[0]) < 1:
        logs = [ log for log in logs if log['severity'] != 'INFO' ]

    # Human-readable output?
    if utils.intify(options.get('debug', ['0'])[0]):
        logs = [ '%(timestamp)d [%(severity)s]\t%(message)s\r\n' % log
                 for log in logs ]
        body = ''.join(logs).encode('utf-8')
        mimetype = 'text/plain; encoding=utf-8'
    else:
        body = json.dumps(logs)
        mimetype = 'application/json'

    # Compose and send response
    response = Message()
    response.compose(code='200', reason='Ok', body=body, mimetype=mimetype)
    stream.send_response(request, response)
Beispiel #4
0
import sys

if __name__ == "__main__":
    sys.path.insert(0, ".")

from neubot.log import LOG, oops
from neubot import compat

if __name__ == "__main__":

    logging.info("INFO w/ logging.info")
    logging.debug("DEBUG w/ logging.debug")
    logging.warning("WARNING w/ logging.warning")
    logging.error("ERROR w/ logging.error")

    print compat.json.dumps(LOG.listify())

    access_logger = logging.getLogger('access')
    access_logger.info('Test access logger')

    try:
        raise Exception("Testing exc_info")
    except (KeyboardInterrupt, SystemExit):
        raise
    except:
        logging.error('EXCEPTION', exc_info=1)

    oops("Testing the new oops feature")

    # Testing variadic args
    logging.warning("WARNING %s", "variadic warning")