Exemple #1
0
    def _emit(self, record):
        if not self.can_record(record):
            return

        hub = Hub.current
        if hub.client is None:
            return

        if self._should_create_event(record):
            with capture_internal_exceptions():
                hint = None
                # exc_info might be None or (None, None, None)
                if record.exc_info is not None and record.exc_info[0] is not None:
                    event, hint = event_from_exception(
                        record.exc_info,
                        with_locals=hub.client.options["with_locals"],
                        mechanism={"type": "logging", "handled": True},
                    )
                else:
                    event = {}

                event["level"] = self._logging_to_event_level(record.levelname)
                event["logger"] = record.name
                event["logentry"] = {
                    "message": to_string(record.msg),
                    "params": record.args,
                }

                hub.capture_event(event, hint=hint)

        with capture_internal_exceptions():
            hub.add_breadcrumb(
                self._breadcrumb_from_record(record), hint={"log_record": record}
            )
    def _emit(self, record):
        # type: (LogRecord) -> None
        if not _can_record(record):
            return

        hub = Hub.current
        if hub.client is None:
            return

        client_options = hub.client.options

        # exc_info might be None or (None, None, None)
        if record.exc_info is not None and record.exc_info[0] is not None:
            event, hint = event_from_exception(
                record.exc_info,
                client_options=client_options,
                mechanism={
                    "type": "logging",
                    "handled": True
                },
            )
        elif record.exc_info and record.exc_info[0] is None:
            event = {}
            hint = {}
            with capture_internal_exceptions():
                event["threads"] = {
                    "values": [{
                        "stacktrace":
                        current_stacktrace(client_options["with_locals"]),
                        "crashed":
                        False,
                        "current":
                        True,
                    }]
                }
        else:
            event = {}
            hint = {}

        hint["log_record"] = record

        client = Hub.current.client

        event["level"] = _logging_to_event_level(record.levelname)
        event["logger"] = record.name
        event["logentry"] = {
            "message":
            to_string(record.msg),
            "params":
            partial_serialize(client, record.args, should_repr_strings=False),
        }
        event["extra"] = partial_serialize(client,
                                           _extra_from_record(record),
                                           should_repr_strings=False)

        hub.capture_event(event, hint=hint)
    def _emit(self, record):
        # type: (LogRecord) -> None
        if not _can_record(record):
            return

        hub = Hub.current
        if hub.client is None:
            return

        client_options = hub.client.options

        # exc_info might be None or (None, None, None)
        #
        # exc_info may also be any falsy value due to Python stdlib being
        # liberal with what it receives and Celery's billiard being "liberal"
        # with what it sends. See
        # https://github.com/getsentry/sentry-python/issues/904
        if record.exc_info and record.exc_info[0] is not None:
            event, hint = event_from_exception(
                record.exc_info,
                client_options=client_options,
                mechanism={
                    "type": "logging",
                    "handled": True
                },
            )
        elif record.exc_info and record.exc_info[0] is None:
            event = {}
            hint = {}
            with capture_internal_exceptions():
                event["threads"] = {
                    "values": [{
                        "stacktrace":
                        current_stacktrace(client_options["with_locals"]),
                        "crashed":
                        False,
                        "current":
                        True,
                    }]
                }
        else:
            event = {}
            hint = {}

        hint["log_record"] = record

        event["level"] = _logging_to_event_level(record.levelname)
        event["logger"] = record.name
        event["logentry"] = {
            "message": to_string(record.msg),
            "params": record.args
        }
        event["extra"] = _extra_from_record(record)

        hub.capture_event(event, hint=hint)
Exemple #4
0
    def _emit(self, record):
        # type: (LogRecord) -> None
        if not _can_record(record):
            return

        hub = Hub.current
        if hub.client is None:
            return

        hint = None  # type: Optional[Dict[str, Any]]
        client_options = hub.client.options

        # exc_info might be None or (None, None, None)
        if record.exc_info is not None and record.exc_info[0] is not None:
            event, hint = event_from_exception(
                record.exc_info,
                client_options=client_options,
                mechanism={
                    "type": "logging",
                    "handled": True
                },
            )
        elif record.exc_info and record.exc_info[0] is None:
            event = {}
            hint = None
            with capture_internal_exceptions():
                event["threads"] = [{
                    "stacktrace":
                    current_stacktrace(client_options["with_locals"]),
                    "crashed":
                    False,
                    "current":
                    True,
                }]
        else:
            event = {}

        event["level"] = _logging_to_event_level(record.levelname)
        event["logger"] = record.name
        event["logentry"] = {
            "message": to_string(record.msg),
            "params": record.args
        }
        event["extra"] = _extra_from_record(record)

        hub.capture_event(event, hint=hint)
Exemple #5
0
def _format_sql(cursor, sql):
    # type: (Any, str) -> Optional[str]

    real_sql = None

    # If we're using psycopg2, it could be that we're
    # looking at a query that uses Composed objects. Use psycopg2's mogrify
    # function to format the query. We lose per-parameter trimming but gain
    # accuracy in formatting.
    try:
        if hasattr(cursor, "mogrify"):
            real_sql = cursor.mogrify(sql)
            if isinstance(real_sql, bytes):
                real_sql = real_sql.decode(cursor.connection.encoding)
    except Exception:
        real_sql = None

    return real_sql or to_string(sql)
Exemple #6
0
    def _emit(self, record):
        # type: (LogRecord) -> None
        if not _can_record(record):
            return

        hub = Hub.current
        if hub.client is None:
            return

        client_options = hub.client.options

        # exc_info might be None or (None, None, None)
        #
        # exc_info may also be any falsy value due to Python stdlib being
        # liberal with what it receives and Celery's billiard being "liberal"
        # with what it sends. See
        # https://github.com/getsentry/sentry-python/issues/904
        if record.exc_info and record.exc_info[0] is not None:
            event, hint = event_from_exception(
                record.exc_info,
                client_options=client_options,
                mechanism={
                    "type": "logging",
                    "handled": True
                },
            )
        elif record.exc_info and record.exc_info[0] is None:
            event = {}
            hint = {}
            with capture_internal_exceptions():
                event["threads"] = {
                    "values": [{
                        "stacktrace":
                        current_stacktrace(
                            client_options["with_locals"],
                            client_options["with_source_context"],
                        ),
                        "crashed":
                        False,
                        "current":
                        True,
                    }]
                }
        else:
            event = {}
            hint = {}

        hint["log_record"] = record

        event["level"] = _logging_to_event_level(record.levelname)
        event["logger"] = record.name

        # Log records from `warnings` module as separate issues
        record_caputured_from_warnings_module = (record.name == "py.warnings"
                                                 and record.msg == "%s")
        if record_caputured_from_warnings_module:
            # use the actual message and not "%s" as the message
            # this prevents grouping all warnings under one "%s" issue
            msg = record.args[0]  # type: ignore

            event["logentry"] = {
                "message": msg,
                "params": (),
            }

        else:
            event["logentry"] = {
                "message": to_string(record.msg),
                "params": record.args,
            }

        event["extra"] = _extra_from_record(record)

        hub.capture_event(event, hint=hint)