def log_exception(exc_info, send_to_sentry=True, **kwargs): """ Add exception info to the log context for the request. We do not log in a separate log statement in order to make debugging easier. As a bonus, this reduces log volume somewhat. """ if send_to_sentry: sentry_alert() if not is_live_env(): print traceback.print_exc() print new_log_context = create_error_log_context(exc_info) new_log_context.update(kwargs) # guard against programming errors overriding log fields (confusing!) if set(new_log_context.keys()).intersection( set(request.environ.get('log_context', {}))): log.warning("attempt to log more than one error to HTTP request", request_uid=get_request_uid(request.headers), **new_log_context) else: request.environ.setdefault('log_context', {}).update(new_log_context)
def log_uncaught_errors(logger=None, **kwargs): """ Helper to log uncaught exceptions. All additional kwargs supplied will be sent to Sentry as extra data. Parameters ---------- logger: structlog.BoundLogger, optional The logging object to write to. """ logger = logger or get_logger() kwargs.update(create_error_log_context(sys.exc_info())) logger.error('Uncaught error', **kwargs) sentry_alert(tags=kwargs)