PHP 8.3.31
Preview: logger.py Size: 2.60 KB
/opt/hc_python/lib64/python3.12/site-packages/sentry_sdk/logger.py

# NOTE: this is the logger sentry exposes to users, not some generic logger.
import functools
import time
from typing import TYPE_CHECKING, Any

import sentry_sdk
from sentry_sdk.utils import capture_internal_exceptions, format_attribute

if TYPE_CHECKING:
    from sentry_sdk._types import Attributes


OTEL_RANGES = [
    # ((severity level range), severity text)
    # https://opentelemetry.io/docs/specs/otel/logs/data-model
    ((1, 4), "trace"),
    ((5, 8), "debug"),
    ((9, 12), "info"),
    ((13, 16), "warn"),
    ((17, 20), "error"),
    ((21, 24), "fatal"),
]


class _dict_default_key(dict):  # type: ignore[type-arg]
    """dict that returns the key if missing."""

    def __missing__(self, key: str) -> str:
        return "{" + key + "}"


def _capture_log(
    severity_text: str, severity_number: int, template: str, **kwargs: "Any"
) -> None:
    body = template

    attributes: "Attributes" = {}

    if "attributes" in kwargs:
        provided_attributes = kwargs.pop("attributes") or {}
        for attribute, value in provided_attributes.items():
            attributes[attribute] = format_attribute(value)

    for k, v in kwargs.items():
        attributes[f"sentry.message.parameter.{k}"] = format_attribute(v)

    if kwargs:
        # only attach template if there are parameters
        attributes["sentry.message.template"] = format_attribute(template)

        with capture_internal_exceptions():
            body = template.format_map(_dict_default_key(kwargs))

    sentry_sdk.get_current_scope()._capture_log(
        {
            "severity_text": severity_text,
            "severity_number": severity_number,
            "attributes": attributes,
            "body": body,
            "time_unix_nano": time.time_ns(),
            "trace_id": None,
            "span_id": None,
        }
    )


trace = functools.partial(_capture_log, "trace", 1)
debug = functools.partial(_capture_log, "debug", 5)
info = functools.partial(_capture_log, "info", 9)
warning = functools.partial(_capture_log, "warn", 13)
error = functools.partial(_capture_log, "error", 17)
fatal = functools.partial(_capture_log, "fatal", 21)


def _otel_severity_text(otel_severity_number: int) -> str:
    for (lower, upper), severity in OTEL_RANGES:
        if lower <= otel_severity_number <= upper:
            return severity

    return "default"


def _log_level_to_otel(level: int, mapping: "dict[Any, int]") -> "tuple[int, str]":
    for py_level, otel_severity_number in sorted(mapping.items(), reverse=True):
        if level >= py_level:
            return otel_severity_number, _otel_severity_text(otel_severity_number)

    return 0, "default"

Directory Contents

Dirs: 5 × Files: 36

Name Size Perms Modified Actions
ai DIR
- drwxr-xr-x 2026-06-11 06:30:31
Edit Download
crons DIR
- drwxr-xr-x 2026-06-11 06:30:31
Edit Download
- drwxr-xr-x 2026-06-11 06:30:31
Edit Download
profiler DIR
- drwxr-xr-x 2026-06-11 06:30:31
Edit Download
- drwxr-xr-x 2026-06-11 06:30:31
Edit Download
15.59 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
2.95 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
49.95 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
61.95 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
959 B lrw-r--r-- 2026-06-11 06:30:30
Edit Download
9.37 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
2.50 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
24.54 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
2.60 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
1.42 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
4.47 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
0 B lrw-r--r-- 2026-06-11 06:30:30
Edit Download
74.09 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
5.99 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
12.82 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
5.08 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
8.59 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
11.85 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
25.08 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
50.33 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
54.36 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
44.41 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
1.24 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
65.96 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
10.91 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
5.70 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
3.00 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
2.43 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
1.88 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
1.14 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
1.21 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
10.98 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
8.12 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
13.16 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
3.85 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download
1.46 KB lrw-r--r-- 2026-06-11 06:30:30
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).