Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SentryLogger.py « SentryLogger « plugins - github.com/Ultimaker/Cura.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0a65e1e00af7709ff4a407653bf7f02c8113cdfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Copyright (c) 2019 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.

from UM.Logger import LogOutput
from typing import Set

from cura.CrashHandler import CrashHandler

try:
    from sentry_sdk import add_breadcrumb
except ImportError:
    pass
from typing import Optional


class SentryLogger(LogOutput):
    # Sentry (https://sentry.io) is the service that Cura uses for logging crashes. This logger ensures that the
    # regular log entries that we create are added as breadcrumbs so when a crash actually happens, they are already
    # processed and ready for sending.
    # Note that this only prepares them for sending. It only sends them when the user actually agrees to sending the
    # information.

    _levels = {
        "w": "warning",
        "i": "info",
        "c": "fatal",
        "e": "error",
        "d": "debug"
    }

    def __init__(self) -> None:
        super().__init__()
        self._show_once = set()  # type: Set[str]

    def log(self, log_type: str, message: str) -> None:
        """Log the message to the sentry hub as a breadcrumb

        :param log_type: "e" (error), "i"(info), "d"(debug), "w"(warning) or "c"(critical) (can postfix with "_once")
        :param message: String containing message to be logged
        """
        level = self._translateLogType(log_type)
        message = CrashHandler.pruneSensitiveData(message)
        if level is None:
            if message not in self._show_once:
                level = self._translateLogType(log_type[0])
                if level is not None:
                    self._show_once.add(message)
                    add_breadcrumb(level = level, message = message)
        else:
            add_breadcrumb(level = level, message = message)

    @staticmethod
    def _translateLogType(log_type: str) -> Optional[str]:
        return SentryLogger._levels.get(log_type)