logging.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. import logging
  2. import sys
  3. from datetime import datetime, timezone
  4. from fnmatch import fnmatch
  5. import sentry_sdk
  6. from sentry_sdk.client import BaseClient
  7. from sentry_sdk.logger import _log_level_to_otel
  8. from sentry_sdk.utils import (
  9. safe_repr,
  10. to_string,
  11. event_from_exception,
  12. current_stacktrace,
  13. capture_internal_exceptions,
  14. has_logs_enabled,
  15. )
  16. from sentry_sdk.integrations import Integration
  17. from typing import TYPE_CHECKING
  18. if TYPE_CHECKING:
  19. from collections.abc import MutableMapping
  20. from logging import LogRecord
  21. from typing import Any
  22. from typing import Dict
  23. from typing import Optional
  24. DEFAULT_LEVEL = logging.INFO
  25. DEFAULT_EVENT_LEVEL = logging.ERROR
  26. LOGGING_TO_EVENT_LEVEL = {
  27. logging.NOTSET: "notset",
  28. logging.DEBUG: "debug",
  29. logging.INFO: "info",
  30. logging.WARN: "warning", # WARN is same a WARNING
  31. logging.WARNING: "warning",
  32. logging.ERROR: "error",
  33. logging.FATAL: "fatal",
  34. logging.CRITICAL: "fatal", # CRITICAL is same as FATAL
  35. }
  36. # Map logging level numbers to corresponding OTel level numbers
  37. SEVERITY_TO_OTEL_SEVERITY = {
  38. logging.CRITICAL: 21, # fatal
  39. logging.ERROR: 17, # error
  40. logging.WARNING: 13, # warn
  41. logging.INFO: 9, # info
  42. logging.DEBUG: 5, # debug
  43. }
  44. # Capturing events from those loggers causes recursion errors. We cannot allow
  45. # the user to unconditionally create events from those loggers under any
  46. # circumstances.
  47. #
  48. # Note: Ignoring by logger name here is better than mucking with thread-locals.
  49. # We do not necessarily know whether thread-locals work 100% correctly in the user's environment.
  50. #
  51. # Events/breadcrumbs and Sentry Logs have separate ignore lists so that
  52. # framework loggers silenced for events (e.g. django.server) can still be
  53. # captured as Sentry Logs.
  54. _IGNORED_LOGGERS = set(
  55. ["sentry_sdk.errors", "urllib3.connectionpool", "urllib3.connection"]
  56. )
  57. _IGNORED_LOGGERS_SENTRY_LOGS = set(
  58. ["sentry_sdk.errors", "urllib3.connectionpool", "urllib3.connection"]
  59. )
  60. def ignore_logger(
  61. name: str,
  62. ) -> None:
  63. """This disables recording (both in breadcrumbs and as events) calls to
  64. a logger of a specific name. Among other uses, many of our integrations
  65. use this to prevent their actions being recorded as breadcrumbs. Exposed
  66. to users as a way to quiet spammy loggers.
  67. This does **not** affect Sentry Logs — use
  68. :py:func:`ignore_logger_for_sentry_logs` for that.
  69. :param name: The name of the logger to ignore (same string you would pass to ``logging.getLogger``).
  70. """
  71. _IGNORED_LOGGERS.add(name)
  72. def ignore_logger_for_sentry_logs(
  73. name: str,
  74. ) -> None:
  75. """This disables recording as Sentry Logs calls to a logger of a
  76. specific name.
  77. :param name: The name of the logger to ignore (same string you would pass to ``logging.getLogger``).
  78. """
  79. _IGNORED_LOGGERS_SENTRY_LOGS.add(name)
  80. def unignore_logger(
  81. name: str,
  82. ) -> None:
  83. """Reverts a previous :py:func:`ignore_logger` call, re-enabling
  84. recording of breadcrumbs and events for the named logger.
  85. :param name: The name of the logger to unignore.
  86. """
  87. _IGNORED_LOGGERS.discard(name)
  88. def unignore_logger_for_sentry_logs(
  89. name: str,
  90. ) -> None:
  91. """Reverts a previous :py:func:`ignore_logger_for_sentry_logs` call,
  92. re-enabling recording of Sentry Logs for the named logger.
  93. :param name: The name of the logger to unignore.
  94. """
  95. _IGNORED_LOGGERS_SENTRY_LOGS.discard(name)
  96. class LoggingIntegration(Integration):
  97. identifier = "logging"
  98. def __init__(
  99. self,
  100. level: "Optional[int]" = DEFAULT_LEVEL,
  101. event_level: "Optional[int]" = DEFAULT_EVENT_LEVEL,
  102. sentry_logs_level: "Optional[int]" = DEFAULT_LEVEL,
  103. ) -> None:
  104. self._handler = None
  105. self._breadcrumb_handler = None
  106. self._sentry_logs_handler = None
  107. if level is not None:
  108. self._breadcrumb_handler = BreadcrumbHandler(level=level)
  109. if sentry_logs_level is not None:
  110. self._sentry_logs_handler = SentryLogsHandler(level=sentry_logs_level)
  111. if event_level is not None:
  112. self._handler = EventHandler(level=event_level)
  113. def _handle_record(self, record: "LogRecord") -> None:
  114. if self._handler is not None and record.levelno >= self._handler.level:
  115. self._handler.handle(record)
  116. if (
  117. self._breadcrumb_handler is not None
  118. and record.levelno >= self._breadcrumb_handler.level
  119. ):
  120. self._breadcrumb_handler.handle(record)
  121. def _handle_sentry_logs_record(self, record: "LogRecord") -> None:
  122. if (
  123. self._sentry_logs_handler is not None
  124. and record.levelno >= self._sentry_logs_handler.level
  125. ):
  126. self._sentry_logs_handler.handle(record)
  127. @staticmethod
  128. def setup_once() -> None:
  129. old_callhandlers = logging.Logger.callHandlers
  130. def sentry_patched_callhandlers(self: "Any", record: "LogRecord") -> "Any":
  131. # keeping a local reference because the
  132. # global might be discarded on shutdown
  133. ignored_loggers = _IGNORED_LOGGERS
  134. ignored_loggers_sentry_logs = _IGNORED_LOGGERS_SENTRY_LOGS
  135. try:
  136. return old_callhandlers(self, record)
  137. finally:
  138. # This check is done twice, once also here before we even get
  139. # the integration. Otherwise we have a high chance of getting
  140. # into a recursion error when the integration is resolved
  141. # (this also is slower).
  142. name = record.name.strip()
  143. handle_events = (
  144. ignored_loggers is not None and name not in ignored_loggers
  145. )
  146. handle_sentry_logs = (
  147. ignored_loggers_sentry_logs is not None
  148. and name not in ignored_loggers_sentry_logs
  149. )
  150. if handle_events or handle_sentry_logs:
  151. integration = sentry_sdk.get_client().get_integration(
  152. LoggingIntegration
  153. )
  154. if integration is not None:
  155. if handle_events:
  156. integration._handle_record(record)
  157. if handle_sentry_logs:
  158. integration._handle_sentry_logs_record(record)
  159. logging.Logger.callHandlers = sentry_patched_callhandlers # type: ignore
  160. class _BaseHandler(logging.Handler):
  161. COMMON_RECORD_ATTRS = frozenset(
  162. (
  163. "args",
  164. "created",
  165. "exc_info",
  166. "exc_text",
  167. "filename",
  168. "funcName",
  169. "levelname",
  170. "levelno",
  171. "linenno",
  172. "lineno",
  173. "message",
  174. "module",
  175. "msecs",
  176. "msg",
  177. "name",
  178. "pathname",
  179. "process",
  180. "processName",
  181. "relativeCreated",
  182. "stack",
  183. "tags",
  184. "taskName",
  185. "thread",
  186. "threadName",
  187. "stack_info",
  188. )
  189. )
  190. def _logging_to_event_level(self, record: "LogRecord") -> str:
  191. return LOGGING_TO_EVENT_LEVEL.get(
  192. record.levelno, record.levelname.lower() if record.levelname else ""
  193. )
  194. def _extra_from_record(self, record: "LogRecord") -> "MutableMapping[str, object]":
  195. return {
  196. k: v
  197. for k, v in vars(record).items()
  198. if k not in self.COMMON_RECORD_ATTRS
  199. and (not isinstance(k, str) or not k.startswith("_"))
  200. }
  201. class EventHandler(_BaseHandler):
  202. """
  203. A logging handler that emits Sentry events for each log record
  204. Note that you do not have to use this class if the logging integration is enabled, which it is by default.
  205. """
  206. def _can_record(self, record: "LogRecord") -> bool:
  207. """Prevents ignored loggers from recording"""
  208. for logger in _IGNORED_LOGGERS:
  209. if fnmatch(record.name.strip(), logger):
  210. return False
  211. return True
  212. def emit(self, record: "LogRecord") -> "Any":
  213. with capture_internal_exceptions():
  214. self.format(record)
  215. return self._emit(record)
  216. def _emit(self, record: "LogRecord") -> None:
  217. if not self._can_record(record):
  218. return
  219. client = sentry_sdk.get_client()
  220. if not client.is_active():
  221. return
  222. client_options = client.options
  223. # exc_info might be None or (None, None, None)
  224. #
  225. # exc_info may also be any falsy value due to Python stdlib being
  226. # liberal with what it receives and Celery's billiard being "liberal"
  227. # with what it sends. See
  228. # https://github.com/getsentry/sentry-python/issues/904
  229. if record.exc_info and record.exc_info[0] is not None:
  230. event, hint = event_from_exception(
  231. record.exc_info,
  232. client_options=client_options,
  233. mechanism={"type": "logging", "handled": True},
  234. )
  235. elif (record.exc_info and record.exc_info[0] is None) or record.stack_info:
  236. event = {}
  237. hint = {}
  238. with capture_internal_exceptions():
  239. event["threads"] = {
  240. "values": [
  241. {
  242. "stacktrace": current_stacktrace(
  243. include_local_variables=client_options[
  244. "include_local_variables"
  245. ],
  246. max_value_length=client_options["max_value_length"],
  247. ),
  248. "crashed": False,
  249. "current": True,
  250. }
  251. ]
  252. }
  253. else:
  254. event = {}
  255. hint = {}
  256. hint["log_record"] = record
  257. level = self._logging_to_event_level(record)
  258. if level in {"debug", "info", "warning", "error", "critical", "fatal"}:
  259. event["level"] = level # type: ignore[typeddict-item]
  260. event["logger"] = record.name
  261. if (
  262. sys.version_info < (3, 11)
  263. and record.name == "py.warnings"
  264. and record.msg == "%s"
  265. ):
  266. # warnings module on Python 3.10 and below sets record.msg to "%s"
  267. # and record.args[0] to the actual warning message.
  268. # This was fixed in https://github.com/python/cpython/pull/30975.
  269. message = record.args[0]
  270. params = ()
  271. else:
  272. message = record.msg
  273. params = record.args
  274. event["logentry"] = {
  275. "message": to_string(message),
  276. "formatted": record.getMessage(),
  277. "params": params,
  278. }
  279. event["extra"] = self._extra_from_record(record)
  280. sentry_sdk.capture_event(event, hint=hint)
  281. # Legacy name
  282. SentryHandler = EventHandler
  283. class BreadcrumbHandler(_BaseHandler):
  284. """
  285. A logging handler that records breadcrumbs for each log record.
  286. Note that you do not have to use this class if the logging integration is enabled, which it is by default.
  287. """
  288. def _can_record(self, record: "LogRecord") -> bool:
  289. """Prevents ignored loggers from recording"""
  290. for logger in _IGNORED_LOGGERS:
  291. if fnmatch(record.name.strip(), logger):
  292. return False
  293. return True
  294. def emit(self, record: "LogRecord") -> "Any":
  295. with capture_internal_exceptions():
  296. self.format(record)
  297. return self._emit(record)
  298. def _emit(self, record: "LogRecord") -> None:
  299. if not self._can_record(record):
  300. return
  301. sentry_sdk.add_breadcrumb(
  302. self._breadcrumb_from_record(record), hint={"log_record": record}
  303. )
  304. def _breadcrumb_from_record(self, record: "LogRecord") -> "Dict[str, Any]":
  305. return {
  306. "type": "log",
  307. "level": self._logging_to_event_level(record),
  308. "category": record.name,
  309. "message": record.message,
  310. "timestamp": datetime.fromtimestamp(record.created, timezone.utc),
  311. "data": self._extra_from_record(record),
  312. }
  313. class SentryLogsHandler(_BaseHandler):
  314. """
  315. A logging handler that records Sentry logs for each Python log record.
  316. Note that you do not have to use this class if the logging integration is enabled, which it is by default.
  317. """
  318. def _can_record(self, record: "LogRecord") -> bool:
  319. """Prevents ignored loggers from recording"""
  320. for logger in _IGNORED_LOGGERS_SENTRY_LOGS:
  321. if fnmatch(record.name.strip(), logger):
  322. return False
  323. return True
  324. def emit(self, record: "LogRecord") -> "Any":
  325. with capture_internal_exceptions():
  326. self.format(record)
  327. if not self._can_record(record):
  328. return
  329. client = sentry_sdk.get_client()
  330. if not client.is_active():
  331. return
  332. if not has_logs_enabled(client.options):
  333. return
  334. self._capture_log_from_record(client, record)
  335. def _capture_log_from_record(
  336. self, client: "BaseClient", record: "LogRecord"
  337. ) -> None:
  338. otel_severity_number, otel_severity_text = _log_level_to_otel(
  339. record.levelno, SEVERITY_TO_OTEL_SEVERITY
  340. )
  341. project_root = client.options["project_root"]
  342. attrs: "Any" = self._extra_from_record(record)
  343. attrs["sentry.origin"] = "auto.log.stdlib"
  344. parameters_set = False
  345. if record.args is not None:
  346. if isinstance(record.args, tuple):
  347. parameters_set = bool(record.args)
  348. for i, arg in enumerate(record.args):
  349. attrs[f"sentry.message.parameter.{i}"] = (
  350. arg
  351. if isinstance(arg, (str, float, int, bool))
  352. else safe_repr(arg)
  353. )
  354. elif isinstance(record.args, dict):
  355. parameters_set = bool(record.args)
  356. for key, value in record.args.items():
  357. attrs[f"sentry.message.parameter.{key}"] = (
  358. value
  359. if isinstance(value, (str, float, int, bool))
  360. else safe_repr(value)
  361. )
  362. if parameters_set and isinstance(record.msg, str):
  363. # only include template if there is at least one
  364. # sentry.message.parameter.X set
  365. attrs["sentry.message.template"] = record.msg
  366. if record.lineno:
  367. attrs["code.line.number"] = record.lineno
  368. if record.pathname:
  369. if project_root is not None and record.pathname.startswith(project_root):
  370. attrs["code.file.path"] = record.pathname[len(project_root) + 1 :]
  371. else:
  372. attrs["code.file.path"] = record.pathname
  373. if record.funcName:
  374. attrs["code.function.name"] = record.funcName
  375. if record.thread:
  376. attrs["thread.id"] = record.thread
  377. if record.threadName:
  378. attrs["thread.name"] = record.threadName
  379. if record.process:
  380. attrs["process.pid"] = record.process
  381. if record.processName:
  382. attrs["process.executable.name"] = record.processName
  383. if record.name:
  384. attrs["logger.name"] = record.name
  385. # noinspection PyProtectedMember
  386. sentry_sdk.get_current_scope()._capture_log(
  387. {
  388. "severity_text": otel_severity_text,
  389. "severity_number": otel_severity_number,
  390. "body": record.message,
  391. "attributes": attrs,
  392. "time_unix_nano": int(record.created * 1e9),
  393. "trace_id": None,
  394. "span_id": None,
  395. },
  396. )