Esempio n. 1
0
class AirbrakeHandler(logging.Handler):

    """
    A handler class which ships logs to airbrake.io

    Requires one:
        * `project_id` AND `api_key`
        * an instance of airbrake.Airbrake
    """

    def __init__(self, airbrake=None, level=logging.ERROR, **kwargs):
        """Initialize the Airbrake handler with a default logging level.

        Default level of logs handled by this class are >= ERROR,
        which includes ERROR and CRITICAL. To change this behavior
        supply a different arguement for 'level'.

        All 'kwargs' will be passed to notifier.Airbrake to instantiate
        a notifier client.
        """

        logging.Handler.__init__(self, level=level)

        if isinstance(airbrake, Airbrake):
            self.airbrake = airbrake
        else:
            self.airbrake = Airbrake(**kwargs)

    def emit(self, record):
        """Log the record airbrake.io style.

        To prevent method calls which invoke this handler from using the
        global exception context in sys.exc_info(), exc_info must be passed
        as False.

        E.g. To prevent AirbrakeHandler from reading the global exception
        context, (which it may do to find a traceback and error type), make
        logger method calls like this:

            LOG.error("Bad math.", exc_info=False)

        Otherwise, provide exception context directly, though the following
        contrived example would be a strange way to use the handler:

            exc_info = sys.exc_info()
            ...
            LOG.error("Bad math.", exc_info=exc_info)
        """
        try:
            airbrakeerror = airbrake_error_from_logrecord(record)
            self.airbrake.log(**airbrakeerror)
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)
Esempio n. 2
0
class AirbrakeHandler(logging.Handler):
    """
    A handler class which ships logs to airbrake.io

    Requires one:
        * `project_id` AND `api_key`
        * an instance of airbrake.Airbrake
    """
    def __init__(self,
                 airbrake=None,
                 level=logging.ERROR,
                 project_id=None,
                 api_key=None,
                 environment=None):
        """Initialize the Airbrake handler with a default logging level.

        Default level of logs handled by this class are >= ERROR,
        which includes ERROR and CRITICAL. To change this behavior
        supply a different argument for 'level'.
        """

        logging.Handler.__init__(self, level=level)

        if isinstance(airbrake, Airbrake):
            self.airbrake = airbrake
        else:
            self.airbrake = Airbrake(project_id, api_key, environment)

    def emit(self, record):
        """Log the record airbrake.io style.

        To prevent method calls which invoke this handler from using the
        global exception context in sys.exc_info(), exc_info must be passed
        as False.

        E.g. To prevent AirbrakeHandler from reading the global exception
        context, (which it may do to find a traceback and error type), make
        logger method calls like this:

            LOG.error("Bad math.", exc_info=False)

        Otherwise, provide exception context directly, though the following
        contrived example would be a strange way to use the handler:

            exc_info = sys.exc_info()
            ...
            LOG.error("Bad math.", exc_info=exc_info)
        """
        try:
            airbrakeerror = airbrake_error_from_logrecord(record)
            self.airbrake.log(**airbrakeerror)
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)
Esempio n. 3
0
class AirbrakeHandler(logging.Handler):
    """
    A handler class which ships logs to airbrake.io

    Requires one:
        * `project_id` AND `api_key`
        * an instance of airbrake.Airbrake
    """
    def __init__(self, project_id=None, api_key=None, environment="dev",
                 notifier=None, use_ssl=True, airbrake=None):

        logging.Handler.__init__(self)

        if isinstance(airbrake, Airbrake):
            airbrake.auto_notify = True
            self.airbrake = airbrake
        else:
            self.airbrake = Airbrake(project_id=project_id, api_key=api_key,
                                     environment=environment, notifier=notifier,
                                     use_ssl=use_ssl, auto_notify=True)

    def emit(self, record):
        try:
            # exception
            exc = None
            if record.exc_info:
                exc = record.exc_info[1]
            if not exc:
                exc = sys.exc_info()[1]
            if exc:
                self.airbrake.log(exc=exc)
                return

            # record
            record = record.getMessage()
            if record:
                self.airbrake.log(record=record)

        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)