Exemple #1
0
def log_exception(name, **kwargs):
    """Logs an exception, along with relevant information such as message,
    traceback, and anything provided pertinent to the situation. This function
    does nothing unless called while an exception is being handled.

    :param name: ``name`` as passed in to :py:func:`logging.getLogger()`.
    :param kwargs: Other keywords may be passed in and will be included in the
                   produced log line.

    """
    type, value, tb = sys.exc_info()
    if not value:
        return
    tb_repr = reprlib.Repr()
    tb_repr.maxstring = 10000
    logger = logging.getLogger(name)
    data = kwargs.copy()
    data['message'] = str(value)
    data['args'] = value.args
    tb_str = ''.join(traceback.format_exception(type, value, tb))
    data_str = ' '.join([
        '='.join((key, log_repr.repr(val)))
        for key, val in sorted(data.items())
    ])
    logger.error('exception:{0}:unhandled {1} traceback={2}'.format(
        type.__name__, data_str, tb_repr.repr(tb_str)))
Exemple #2
0
    tb_repr = reprlib.Repr()
    tb_repr.maxstring = 10000
    logger = logging.getLogger(name)
    data = kwargs.copy()
    data['message'] = str(value)
    data['args'] = value.args
    tb_str = ''.join(traceback.format_exception(type, value, tb))
    data_str = ' '.join([
        '='.join((key, log_repr.repr(val)))
        for key, val in sorted(data.items())
    ])
    logger.error('exception:{0}:unhandled {1} traceback={2}'.format(
        type.__name__, data_str, tb_repr.repr(tb_str)))


log_repr = reprlib.Repr()
log_repr.maxstring = 100
log_repr.maxother = 100


def logline(log, type, typeid, operation, **data):
    if not data:
        log('{0}:{1}:{2}'.format(type, typeid, operation))
    else:
        data_str = ' '.join([
            '='.join((key, log_repr.repr(val)))
            for key, val in sorted(data.items())
        ])
        log('{0}:{1}:{2} {3}'.format(type, typeid, operation, data_str))