Пример #1
0
    def emit(self, record: logging.LogRecord):
        """Add a logging message."""

        msg = record.msg
        if isinstance(record.msg, utils.LogMessage):
            # Ensure we don't use the extra ASCII indents here.
            record.msg = record.msg.format_msg()

        self.widget['state'] = "normal"
        # We don't want to indent the first line.
        firstline, *lines = self.format(record).split('\n')

        self.widget.insert(
            END,
            # Start with a newline so it doesn't end with one.
            '\n' + firstline,
            (record.levelname,),
        )
        for line in lines:
            self.widget.insert(
                END,
                '\n' + line,
                # Indent following lines.
                (record.levelname, 'INDENT'),
            )
        self.widget.see(END)  # Scroll to the end
        self.widget['state'] = "disabled"

        # Undo the record overwrite, so other handlers get the correct object.
        record.msg = msg
    def format(self, record: logging.LogRecord) -> str:
        # record is a LogRecord
        # https://docs.python.org/3.4/library/logging.html#logging.LogRecord

        # message = super().format(record)
        super().format(record)
        # Since fmt does not contain asctime, the Formatter.format()
        # will not write asctime (since its usesTime()) function will be
        # false. Therefore:
        record.asctime = self.formatTime(record, self.datefmt)
        bg_col = self.log_background_colors[record.levelno]
        msg = escape(record.getMessage())
        # escape() won't replace \n but will replace & etc.
        if self.replace_nl_with_br:
            msg = msg.replace("\n", "<br>")
        html = (
            '<span style="color:#008B8B">{time}.{ms:03d} {name}:{lvname}: '
            '</span><span style="color:{color}{bg}">{msg}</font>{br}'.format(
                time=record.asctime,
                ms=int(record.msecs),
                name=record.name,
                lvname=record.levelname,
                color=self.log_colors[record.levelno],
                msg=msg,
                bg=";background-color:{}".format(bg_col) if bg_col else "",
                br="<br>" if self.append_br else "",
            )
        )
        # print("record.__dict__: {}".format(record.__dict__))
        # print("html: {}".format(html))
        return html
Пример #3
0
    def format(self, record: logging.LogRecord):
        """
        Format the specified record as text.

        This implementation is directly copied from the superclass,
        only adding the codeblock to the traceback.
        """

        record.message = clean_content(record.getMessage())

        if self.usesTime():
            record.asctime = self.formatTime(record, self.datefmt)

        s = self.formatMessage(record)

        if record.exc_info:
            # Cache the traceback text to avoid converting it multiple times
            # (it's constant anyway)
            if not record.exc_text:
                record.exc_text = self.formatException(record.exc_info)

        if record.exc_text:
            if s[-1:] != "\n":
                s = s + "\n"

            # add a codeblock so the DiscordHandler can properly split
            # the error into multiple messages if needed
            s = f'{s}```py\n{record.exc_text}```'

        if record.stack_info:
            if s[-1:] != "\n":
                s = s + "\n"
            s = s + self.formatStack(record.stack_info)

        return s
Пример #4
0
 def record(self, app_name):
     self.record_count += 1
     msg =  "log msg %r" %(self.record_count, )
     record = LogRecord("name", logging.INFO, "x.py", 42, msg, None, None)
     record.app_name = app_name
     record.created = 100 + self.record_count
     record.msecs = 42
     return record
Пример #5
0
 def log_and_publish(self, message, *args, **kwargs):
     """Log a line using and send it to the web tier.
     The *args are passed to logger.info()
     The **kwargs are passed to '_send_line()'
     """
     logger.info(message, *args)
     record = LogRecord('name', logging.INFO, "(unknown file)", 0,
                        message, args, exc_info=None, func=None)
     full_message = record.getMessage()
     self.publish_message(line=full_message, **kwargs)
Пример #6
0
 def makeRecord(self, name, level, fn, lno, msg, args, exc_info,
         func=None, extra=None):
     rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
     if extra is not None:
         for key, value in extra.items():
             if key in ("message", "asctime") or key in rv.__dict__:
                 raise KeyError(
                         "Attempt to override %r in LogRecord" % key)
             rv.__dict__[key] = value
     rv.processName = multiprocessing.current_process()._name
     return rv
Пример #7
0
 def log_and_publish_error(self, message, *args, **kwargs):
     """Log a line using and send it to the web tier.
     The *args are passed to logger.info()
     The **kwargs are passed to '_send_line()'
     """
     logger.error(message, *args)
     record = LogRecord('name', logging.ERROR, "(unknown file)", 0,
                        message, args, exc_info=None, func=None)
     full_message = record.getMessage()
     updated_kwargs = dict(kwargs)
     updated_kwargs['errorLine'] = True
     self.publish_message(line=full_message, **updated_kwargs)
Пример #8
0
def test_json_formatter_easy_extra_types(json_formatter):
    record = LogRecord(
        name='test', level=INFO, pathname='/test.py', lineno=1337, msg="This is a test", args=[], exc_info=None
    )
    record.extra_one = 'String Test'
    record.extra_two = 1092384901283490120984
    record.extra_three = ['list test']
    actual = json.loads(json_formatter.format(record))
    assert_contains(actual, {
        'extra_one': 'String Test',
        'extra_two': 1092384901283490120984,
        'extra_three': ['list test'],
    })
Пример #9
0
def test_json_formatter_non_easy_extra_types(json_formatter):
    record = LogRecord(
        name='test', level=INFO, pathname='/test.py', lineno=1337, msg="This is a test", args=[], exc_info=None
    )

    class Example(object):
        def __repr__(self):
            return 'Example Repr in action!'
    record.extra_one = Example()

    actual = json.loads(json_formatter.format(record))
    assert_contains(actual, {
        'extra_one': 'Example Repr in action!',
    })
Пример #10
0
 def filter(self, record: logging.LogRecord) -> bool:
     """ Decide whether the record should be logged. """
     message = record.getMessage()
     is_new_message = message not in self.__messages_seen
     if is_new_message:
         self.__messages_seen.add(message)
     return is_new_message
Пример #11
0
 def add_logentry(self, record: logging.LogRecord):
     model = self.builder.get_object('logstore')
     assert isinstance(model, Gtk.ListStore)
     if record.levelno == logging.DEBUG:
         textcolor = 'gray'
         bgcolor = 'white'
     elif record.levelno == logging.INFO:
         textcolor = 'black'
         bgcolor = 'white'
     elif record.levelno == logging.WARNING:
         textcolor = 'orange'
         bgcolor = 'white'
     elif record.levelno == logging.ERROR:
         textcolor = 'red'
         bgcolor = 'white'
     elif record.levelno == logging.CRITICAL:
         textcolor = 'black'
         bgcolor = 'red'
     else:
         textcolor = 'blue'
         bgcolor = 'white'
     it = model.append(
         [record.asctime, record.levelname, record.levelno, '{}:{:d}'.format(record.name, record.lineno),
          record.getMessage(), textcolor, bgcolor, record.levelno >= self.filterlevel])
     while len(model) > self.nentries:
         model.remove(model.get_iter_first())
     if self.builder.get_object('autoscroll_checkbutton').get_active():
         filteredmodel = self.builder.get_object('logstore_filtered')
         assert isinstance(filteredmodel, Gtk.TreeModelFilter)
         success, it = filteredmodel.convert_child_iter_to_iter(it)
         if success:
             self.builder.get_object('logview').scroll_to_cell(filteredmodel.get_path(it), None, True, 0.0, 1.0)
     self.update_shown_count()
Пример #12
0
 def format_message(self, record: logging.LogRecord):  # noqa: C901
     s = record.getMessage()
     if record.exc_info:
         # Cache the traceback text to avoid converting it multiple times
         # (it's constant anyway)
         if not record.exc_text:
             record.exc_text = self.formatException(record.exc_info)
     if record.exc_text:
         if s[-1:] != '\n':
             s = s + '\n'
         s = s + record.exc_text
     if record.stack_info:
         if s[-1:] != '\n':
             s = s + '\n'
         s = s + self.formatStack(record.stack_info)
     return s
Пример #13
0
 def filter(self, record: logging.LogRecord) -> bool:
     if record.exc_info:
         self.logged_stack = True
     message = record.getMessage()
     if self.regex.match(message):
         self.matched = True
         return False
     return True
Пример #14
0
    def _format_record(self, record: logging.LogRecord=None) -> logging.LogRecord:
        """
        Formatted the LogRecord
        :param record: LogRecord with the message info
        :return: LogRecord with formatted message
        """
        # ensure that exc_info and args
        # have been stringified. Removes any chance of
        # unpickleable things inside and possibly reduces
        # message size sent over the pipe.
        if record.args:
            record.msg = record.msg % record.args
            record.args = None
        if record.exc_info:
            self.format(record)
            record.exc_info = None

        return record
Пример #15
0
    def emit(self, record: logging.LogRecord):
        """Add a logging message."""

        msg = record.msg
        if isinstance(record.msg, srctools.logger.LogMessage):
            # Ensure we don't use the extra ASCII indents here.
            record.msg = record.msg.format_msg()

        self.widget['state'] = "normal"
        # We don't want to indent the first line.
        firstline, *lines = self.format(record).split('\n')

        if self.has_text:
            # Start with a newline so it doesn't end with one.
            self.widget.insert(
                END,
                '\n',
                (),
            )

        self.widget.insert(
            END,
            firstline,
            (record.levelname,),
        )
        for line in lines:
            self.widget.insert(
                END,
                '\n',
                ('INDENT',),
                line,
                # Indent following lines.
                (record.levelname, 'INDENT'),
            )
        self.widget.see(END)  # Scroll to the end
        self.widget['state'] = "disabled"
        # Update it, so it still runs even when we're busy with other stuff.
        self.widget.update_idletasks()

        self.has_text = True

        # Undo the record overwrite, so other handlers get the correct object.
        record.msg = msg
def test_extra_formatter_formats_log_with_extra():
    format_string = "{message}"
    formatter = ExtraConsoleFormatter(format_string)

    log_message = "A sample log with extra: {foo}."
    extra = dict(foo='bar')

    log_record = LogRecord(
        'name',
        INFO,
        'some_function',
        42,
        log_message,
        None,
        None
    )
    log_record.foo = extra['foo']

    log_result = formatter.format(log_record)
    assert_that(log_result, is_(equal_to(log_message.format(**extra))))
def test_extra_formatter_supports_old_and_new_formats():
    format_string = "{message}"
    formatter = ExtraConsoleFormatter(format_string)

    log_message = "A sample log with old string %s, new: {foo}"
    args = ("bar",)
    extra = dict(foo="baz")

    log_record = LogRecord(
        'name',
        INFO,
        'some_function',
        42,
        log_message,
        args,
        0
    )
    log_record.foo = extra['foo']

    log_result = formatter.format(log_record)
    assert_that(log_result, is_(equal_to(log_message.format(**extra) % args)))
Пример #18
0
    def makeRecord(self, name, level, fn, lno, msg, args, exc_info, func=None, extra=None):
        record = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
        if exc_info:
            if extra is None:
                extra = {}
            # get additional info about the exception
            ex_type, ex, _ = exc_info
            extra["exc_type"] = ex_type.__name__
            # add members of the exception as extra info except callables and non-public stuff
            for name, val in inspect.getmembers(ex, lambda a: not callable(a)):
                # args just contains all exception args again, can be ignored
                if not name.startswith("_") and name not in ("args",):
                    # rename keys which collide with names from the log record
                    if name in ["message", "asctime"] or hasattr(record, name):
                        extra["exc_" + name] = val
                    else:
                        extra[name] = val

            if issubclass(ex_type, UnicodeError):
                obj = extra["object"]

                if isinstance(obj, str):
                    # escape string that caused this exception
                    start = extra["start"]
                    end = extra["end"]
                    snippet_start = start - 1000 if start > 1000 else start
                    snippet_end = end + 1000

                    error_part = obj[start:end + 1].encode("string_escape")
                    extra["object"] = obj[snippet_start:start - 1] + "[ERROR]" + error_part + "[/ERROR]" + obj[end + 1:snippet_end]
                else:
                    extra["object"] = obj[:2000]

        if extra is not None:
            for key in extra:
                if (key in ["message", "asctime"]) or (key in record.__dict__):
                    raise KeyError("Attempt to overwrite %r in LogRecord" % key)
                record.__dict__[key] = extra[key]

        return record
Пример #19
0
    def format(self, record: logging.LogRecord):
        now = datetime.now(tz=self._tz)
        data = dict(
            context=self._context,
            name=self._name,
            message=record.getMessage(),
            level=record.levelname,
            date=now,
            color_start="",
            color_end="",
        )
        if self._terminal:
            data['color_start'] = TerminalColor.for_level(record.levelno)
            if data['color_start']:
                data['color_end'] = colorama.Style.RESET_ALL

        s = (
            "{context}/{name}"
            " [{date:%Y-%m-%d %H:%M:%S %z}]"
            " {color_start}| "
            "{level:{level_name_length}}"
            " | {message}"
            "{color_end}"
        ).format(level_name_length=self._max_logging_level_length, **data)
        if record.exc_info:
            # Cache the traceback text to avoid converting it multiple times
            # (it's constant anyway)
            if not record.exc_text:
                record.exc_text = self.formatException(record.exc_info)
        if record.exc_text:
            if s[-1:] != "\n":
                s += "\n"
            s = s + record.exc_text
        if record.stack_info:
            if s[-1:] != "\n":
                s += "\n"
            s = s + self.formatStack(record.stack_info)
        return s
Пример #20
0
    def emit(self, record: logging.LogRecord) -> None:
        report = {}  # type: Dict[str, Any]

        try:
            report['node'] = platform.node()
            report['host'] = platform.node()

            add_deployment_metadata(report)

            if record.exc_info:
                stack_trace = ''.join(traceback.format_exception(*record.exc_info))
                message = str(record.exc_info[1])
            else:
                stack_trace = 'No stack trace available'
                message = record.getMessage()
                if '\n' in message:
                    # Some exception code paths in queue processors
                    # seem to result in super-long messages
                    stack_trace = message
                    message = message.split('\n')[0]
            report['stack_trace'] = stack_trace
            report['message'] = message

            report['logger_name'] = record.name
            report['log_module'] = find_log_caller_module(record)
            report['log_lineno'] = record.lineno

            if hasattr(record, "request"):
                add_request_metadata(report, record.request)  # type: ignore  # record.request is added dynamically

        except Exception:
            report['message'] = "Exception in preparing exception report!"
            logging.warning(report['message'], exc_info=True)
            report['stack_trace'] = "See /var/log/zulip/errors.log"

        try:
            if settings.STAGING_ERROR_NOTIFICATIONS:
                # On staging, process the report directly so it can happen inside this
                # try/except to prevent looping
                from zerver.lib.error_notify import notify_server_error
                notify_server_error(report)
            else:
                queue_json_publish('error_reports', dict(
                    type = "server",
                    report = report,
                ))
        except Exception:
            # If this breaks, complain loudly but don't pass the traceback up the stream
            # However, we *don't* want to use logging.exception since that could trigger a loop.
            logging.warning("Reporting an exception triggered an exception!", exc_info=True)
Пример #21
0
def _shorten_name_of_logrecord(record: logging.LogRecord) -> logging.LogRecord:
    record = copy.copy(record)  # avoid mutating arg
    # strip the main module name from the logger name
    if record.name.startswith("vialectrum."):
        record.name = record.name[13:]
    # manual map to shorten common module names
    record.name = record.name.replace("interface.Interface", "interface", 1)
    record.name = record.name.replace("network.Network", "network", 1)
    record.name = record.name.replace("synchronizer.Synchronizer", "synchronizer", 1)
    record.name = record.name.replace("verifier.SPV", "verifier", 1)
    record.name = record.name.replace("gui.qt.main_window.ElectrumWindow", "gui.qt.main_window", 1)
    return record
Пример #22
0
    def handle(self, record: logging.LogRecord):
        """ Do whatever it takes to actually log the specified logging record.

        :param logging.LogRecord record: Log record instance to emit
        :return None:
        """
        with self._lock:
            if record.levelno == LEVEL_PROGRESS:
                record.levelname = LEVEL_NAME_PROGRESS
                self.__enable_cr_mode()
                try:
                    padding = ' ' * (self.__terminal_width - len(self.handler.format(record)))
                except:
                    padding = ''
                record.msg += padding

            elif self.__in_cr_mode:
                self.__disable_cr_mode()
                self.handler.stream.write(self.handler.terminator)

            self.handler.handle(record)
Пример #23
0
 def filter(self, record: logging.LogRecord):
     name = package_name.shorten(record.name, 35)
     record.msg = name + " | " + str(record.msg)
     return True
Пример #24
0
def write(self, msg):
    self.emit(LogRecord('run_cmd', logging.INFO, __file__, 0, msg, '', ''))
Пример #25
0
def test_logging_handler_dont_emit_elasticapm(capsys, elasticapm_client):
    handler = LoggingHandler(elasticapm_client)
    handler.emit(LogRecord("elasticapm.errors", 1, "/ab/c/", 10, "Oops", [], None))
    out, err = capsys.readouterr()
    assert "Oops" in err
Пример #26
0
    def filter(self, record: logging.LogRecord) -> bool:
        """Hide sensitive data in messages."""
        record.msg = record.msg.replace(self.text, "*******")

        return True
Пример #27
0
    def emit(self, record: logging.LogRecord) -> None:
        report: Dict[str, Any] = {}

        # This parameter determines whether Zulip should attempt to
        # send Zulip messages containing the error report.  If there's
        # syntax that makes the Markdown processor throw an exception,
        # we really don't want to send that syntax into a new Zulip
        # message in exception handler (that's the stuff of which
        # recursive exception loops are made).
        #
        # We initialize is_markdown_rendering_exception to `True` to
        # prevent the infinite loop of Zulip messages by ERROR_BOT if
        # the outer try block here throws an exception before we have
        # a chance to check the exception for whether it comes from
        # markdown.
        is_markdown_rendering_exception = True

        try:
            report['node'] = platform.node()
            report['host'] = platform.node()

            report['deployment_data'] = dict(
                git=try_git_describe(),
                ZULIP_VERSION=ZULIP_VERSION,
            )

            if record.exc_info:
                stack_trace = ''.join(
                    traceback.format_exception(*record.exc_info))
                message = str(record.exc_info[1])
                is_markdown_rendering_exception = record.msg.startswith(
                    'Exception in Markdown parser')
            else:
                stack_trace = 'No stack trace available'
                message = record.getMessage()
                if '\n' in message:
                    # Some exception code paths in queue processors
                    # seem to result in super-long messages
                    stack_trace = message
                    message = message.split('\n')[0]
                is_markdown_rendering_exception = False
            report['stack_trace'] = stack_trace
            report['message'] = message

            report['logger_name'] = record.name
            report['log_module'] = find_log_caller_module(record)
            report['log_lineno'] = record.lineno

            if isinstance(record, HasRequest):
                add_request_metadata(report, record.request)

        except Exception:
            report['message'] = "Exception in preparing exception report!"
            logging.warning(report['message'], exc_info=True)
            report['stack_trace'] = "See /var/log/zulip/errors.log"
            capture_exception()

        if settings.DEBUG_ERROR_REPORTING:  # nocoverage
            logging.warning("Reporting an error to admins...")
            logging.warning(
                "Reporting an error to admins: %s %s %s %s %s",
                record.levelname,
                report['logger_name'],
                report['log_module'],
                report['message'],
                report['stack_trace'],
            )

        try:
            if settings.STAGING_ERROR_NOTIFICATIONS:
                # On staging, process the report directly so it can happen inside this
                # try/except to prevent looping
                from zerver.lib.error_notify import notify_server_error
                notify_server_error(report, is_markdown_rendering_exception)
            else:
                queue_json_publish('error_reports',
                                   dict(
                                       type="server",
                                       report=report,
                                   ))
        except Exception:
            # If this breaks, complain loudly but don't pass the traceback up the stream
            # However, we *don't* want to use logging.exception since that could trigger a loop.
            logging.warning("Reporting an exception triggered an exception!",
                            exc_info=True)
            capture_exception()
Пример #28
0
 def format(self, record: LogRecord) -> str:
     record.name = self._shorten_module_name(record.name)
     record.args = self._redact_logging_args(record.args)
     return super().format(record)
Пример #29
0
 def emit(self, record: logging.LogRecord) -> None:  # pragma: no cover
     """Intercept standard logging messages toward Loguru sink."""
     logger_opt = logger.opt(depth=7, exception=record.exc_info)
     logger_opt.log(record.levelname, record.getMessage())
Пример #30
0
def health_check_filter(record: LogRecord) -> bool:
    # Filter out health checks from the logs, they're verbose and happen frequently
    return not ("GET /healthcheck" in record.getMessage()
                and record.status_code == 200)
Пример #31
0
 def filter(self, record: logging.LogRecord) -> bool:
     if self.prefix:
         record.msg = self.prefix + ' ' + record.msg
     return True
Пример #32
0
 def filter(self, record: logging.LogRecord) -> bool:
     record.skip_warningsiserror = True  # type: ignore
     return True
Пример #33
0
 def filter(self, record: logging.LogRecord) -> bool:
     return ("were not used when initializing T5EncoderModel: ['decoder."
             not in record.getMessage())
Пример #34
0
 def emit(self, record: logging.LogRecord) -> None:  # pragma: no cover
     logger_opt = logger.opt(depth=7, exception=record.exc_info)
     logger_opt.log(record.levelname, record.getMessage())
Пример #35
0
 def filter(self, record: logging.LogRecord):
     rid = context.get_request_id()
     cid = context.get_correlation_id()
     record.msg = f"RID: {rid} | CID: {cid} | {str(record.msg)}"
     return True
Пример #36
0
def cherrypy_filter(record: logging.LogRecord) -> int:
    blocked = ['TLSV1_ALERT_DECRYPT_ERROR']
    msg = record.getMessage()
    return not any([m for m in blocked if m in msg])
Пример #37
0
 def match(self, record: logging.LogRecord):
     match_name = bool(re.match(self.name_regexp, record.name))
     match_regexp = bool(re.match(self.msg_regexp, record.getMessage()))
     return match_name and match_regexp
Пример #38
0
    def emit(self, record: logging.LogRecord) -> None:
        report: Dict[str, Any] = {}

        try:
            report["node"] = platform.node()
            report["host"] = platform.node()

            report["deployment_data"] = dict(
                git=try_git_describe(),
                ZULIP_VERSION=ZULIP_VERSION,
            )

            if record.exc_info:
                stack_trace = "".join(
                    traceback.format_exception(*record.exc_info))
                message = str(record.exc_info[1])
            else:
                stack_trace = "No stack trace available"
                message = record.getMessage()
                if "\n" in message:
                    # Some exception code paths in queue processors
                    # seem to result in super-long messages
                    stack_trace = message
                    message = message.split("\n")[0]
            report["stack_trace"] = stack_trace
            report["message"] = message

            report["logger_name"] = record.name
            report["log_module"] = find_log_caller_module(record)
            report["log_lineno"] = record.lineno

            if isinstance(record, HasRequest):
                add_request_metadata(report, record.request)

        except Exception:
            report["message"] = "Exception in preparing exception report!"
            logging.warning(report["message"], exc_info=True)
            report["stack_trace"] = "See /var/log/zulip/errors.log"
            capture_exception()

        if settings.DEBUG_ERROR_REPORTING:  # nocoverage
            logging.warning("Reporting an error to admins...")
            logging.warning(
                "Reporting an error to admins: %s %s %s %s %s",
                record.levelname,
                report["logger_name"],
                report["log_module"],
                report["message"],
                report["stack_trace"],
            )

        try:
            queue_json_publish(
                "error_reports",
                dict(
                    type="server",
                    report=report,
                ),
            )
        except Exception:
            # If this breaks, complain loudly but don't pass the traceback up the stream
            # However, we *don't* want to use logging.exception since that could trigger a loop.
            logging.warning("Reporting an exception triggered an exception!",
                            exc_info=True)
            capture_exception()
Пример #39
0
    def filter(self, record: logging.LogRecord):
        msg = record.getMessage()

        return self._filter.match(msg) is not None
Пример #40
0
def record():
    return LogRecord("foo", "INFO", "foo", 10, "waagh :(", (), None)
Пример #41
0
 def filter(self, record: LogRecord):
     record.function = self.function or ""
     return True
Пример #42
0
 def formatMessage(self, Record: logging.LogRecord) -> None:
     lvl: str = Record.levelname
     wanted_color: str = self.colors.get(lvl, self.default_color)
     Record.levelname = f"{wanted_color}{lvl}{self.default_color}"
     return self._style.format(Record)
Пример #43
0
 def format(self, record: logging.LogRecord) -> str:
     levelname = record.levelname
     if levelname in self.COLORS:
         record.levelname = self._to(self.COLORS[levelname], levelname)
     return logging.Formatter.format(self, record)
Пример #44
0
 def _formatTime(self, record: logging.LogRecord) -> None:
     if self.usesTime():
         record.asctime = self.formatTime(record, self.datefmt)
Пример #45
0
    def filter(self, record: logging.LogRecord) -> bool:
        """Hide sensitive data in messages."""
        record.msg = record.msg.replace(self.text, '*******')

        return True
Пример #46
0
 def filter(self, record: logging.LogRecord) -> bool:
     for skip in self.skips:
         if record.getMessage().startswith(skip):
             return False
     return True
Пример #47
0
 def emit(self, record: logging.LogRecord) -> None:
     """Send a log message."""
     self.slack.chat_postMessage(channel=self.channel,
                                 text=record.getMessage())
Пример #48
0
    def emit(self, record: logging.LogRecord) -> None:
        report = {}  # type: Dict[str, Any]

        # This parameter determines whether Zulip should attempt to
        # send Zulip messages containing the error report.  If there's
        # syntax that makes the markdown processor throw an exception,
        # we really don't want to send that syntax into a new Zulip
        # message in exception handler (that's the stuff of which
        # recursive exception loops are made).
        #
        # We initialize is_bugdown_rendering_exception to `True` to
        # prevent the infinite loop of zulip messages by ERROR_BOT if
        # the outer try block here throws an exception before we have
        # a chance to check the exception for whether it comes from
        # bugdown.
        is_bugdown_rendering_exception = True

        try:
            report['node'] = platform.node()
            report['host'] = platform.node()

            add_deployment_metadata(report)

            if record.exc_info:
                stack_trace = ''.join(traceback.format_exception(*record.exc_info))
                message = str(record.exc_info[1])
                from zerver.lib.exceptions import BugdownRenderingException
                is_bugdown_rendering_exception = record.msg.startswith('Exception in Markdown parser')
            else:
                stack_trace = 'No stack trace available'
                message = record.getMessage()
                if '\n' in message:
                    # Some exception code paths in queue processors
                    # seem to result in super-long messages
                    stack_trace = message
                    message = message.split('\n')[0]
                is_bugdown_rendering_exception = False
            report['stack_trace'] = stack_trace
            report['message'] = message

            report['logger_name'] = record.name
            report['log_module'] = find_log_caller_module(record)
            report['log_lineno'] = record.lineno

            if hasattr(record, "request"):
                add_request_metadata(report, record.request)  # type: ignore  # record.request is added dynamically

        except Exception:
            report['message'] = "Exception in preparing exception report!"
            logging.warning(report['message'], exc_info=True)
            report['stack_trace'] = "See /var/log/zulip/errors.log"

        if settings.DEBUG_ERROR_REPORTING:  # nocoverage
            logging.warning("Reporting an error to admins...")
            logging.warning("Reporting an error to admins: {} {} {} {} {}" .format(
                record.levelname, report['logger_name'], report['log_module'],
                report['message'], report['stack_trace']))

        try:
            if settings.STAGING_ERROR_NOTIFICATIONS:
                # On staging, process the report directly so it can happen inside this
                # try/except to prevent looping
                from zerver.lib.error_notify import notify_server_error
                notify_server_error(report, is_bugdown_rendering_exception)
            else:
                queue_json_publish('error_reports', dict(
                    type = "server",
                    report = report,
                ))
        except Exception:
            # If this breaks, complain loudly but don't pass the traceback up the stream
            # However, we *don't* want to use logging.exception since that could trigger a loop.
            logging.warning("Reporting an exception triggered an exception!", exc_info=True)
Пример #49
0
 def filter_(record: logging.LogRecord) -> bool:
     record.processName = process_name
     return True
Пример #50
0
 def filter(self, record: logging.LogRecord) -> bool:
     # NOTE: space is important after `GET /`, else all logs will be disabled.
     return record.getMessage().find("GET / ") == -1
Пример #51
0
 def emit(self, record: logging.LogRecord):
     dispatcher = self.dispatchers.get(record.levelno, self.vim.echom)
     dispatcher(record.getMessage())
 def filter(self, record: logging.LogRecord):
     for attr in self._remove:
         if hasattr(record, attr):
             record.__delattr__(attr)
     return True
 def filter(self, record: logging.LogRecord) -> bool:
     record.time_filter = datetime.datetime.utcnow().strftime(
         "%Y-%m-%d %H:%M:%S.%f")
     return True
Пример #54
0
 def filter(record: logging.LogRecord) -> bool:
     """Removes exception stack traces information."""
     record.exc_info = None
     record.exc_text = None  # type: ignore
     return True
Пример #55
0
 def _format_line(record: logging.LogRecord, msg: str) -> str:
     record = copy(record)
     record.msg = msg
     record.args = ()
     return _super_format(record)
Пример #56
0
    def format(self, record: logging.LogRecord) -> str:
        """

        :param record:
        :return:
        """
        """
            Attribute name 	Format 	Description
            args 	You shouldn’t need to format this yourself. 	The tuple of arguments merged into msg to produce message, or a dict whose values are used for the merge (when there is only one argument, and it is a dictionary).
            asctime 	%(asctime)s 	Human-readable time when the LogRecord was created. By default this is of the form ‘2003-07-08 16:49:45,896’ (the numbers after the comma are millisecond portion of the time).
            created 	%(created)f 	Time when the LogRecord was created (as returned by time.time()).
            exc_info 	You shouldn’t need to format this yourself. 	Exception tuple (à la sys.exc_info) or, if no exception has occurred, None.
            filename 	%(filename)s 	Filename portion of pathname.
            funcName 	%(funcName)s 	Name of function containing the logging call.
            levelname 	%(levelname)s 	Text logging level for the message ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL').
            levelno 	%(levelno)s 	Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL).
            lineno 	%(lineno)d 	Source line number where the logging call was issued (if available).
            module 	%(module)s 	Module (name portion of filename).
            msecs 	%(msecs)d 	Millisecond portion of the time when the LogRecord was created.
            message 	%(message)s 	The logged message, computed as msg % args. This is set when Formatter.format() is invoked.
            msg 	You shouldn’t need to format this yourself. 	The format string passed in the original logging call. Merged with args to produce message, or an arbitrary object (see Using arbitrary objects as messages).
            name 	%(name)s 	Name of the config_logger used to log the call.
            pathname 	%(pathname)s 	Full pathname of the source file where the logging call was issued (if available).
            process 	%(process)d 	Process ID (if available).
            processName 	%(processName)s 	Process name (if available).
            relativeCreated 	%(relativeCreated)d 	Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded.
            stack_info 	You shouldn’t need to format this yourself. 	Stack frame information (where available) from the bottom of the stack in the current thread, up to and including the stack frame of the logging call which resulted in the creation of this record.
            thread 	%(thread)d 	Thread ID (if available).
            threadName 	%(threadName)s 	Thread name (if available).
            
            [service.randomtrailers.backend:DiscoverTmdbMovies:process_page] 
            [service.randomtrailers.backend:FolderMovieData:add_to_discovered_trailers  TRACE_DISCOVERY]
        """
        # threadName Constants.CURRENT_ADDON_SHORT_NAME funcName:lineno
        # [threadName name funcName:lineno]

        text = ''
        try:
            start_file = record.__dict__.get('start_file', None)
            try:
                pathname, lineno, func = start_file
            except ValueError:
                pathname, lineno, func = "(unknown file)", 0, "(unknown function)"

            record.pathname = pathname
            try:
                record.filename = os.path.basename(pathname)
                record.module = os.path.splitext(record.filename)[0]
            except (TypeError, ValueError, AttributeError):
                record.filename = pathname
                record.module = "Unknown module"
            record.lineno = lineno
            record.funcName = func

            suffix = super().format(record)
            passed_traces = record.__dict__.get('trace_string', None)
            if passed_traces is None:
                if type(self).INCLUDE_THREAD_INFO:
                    prefix = '[Thread {!s} {!s}.{!s}:{!s}:{!s}]'.format(
                        record.threadName, record.name, record.funcName,
                        record.lineno, record.levelname)
                else:
                    prefix = '[{!s}.{!s}:{!s}]'.format(record.name,
                                                       record.funcName,
                                                       record.lineno)
            else:
                if type(self).INCLUDE_THREAD_INFO:
                    prefix = '[Thread {!s} {!s}.{!s}:{!s}:{!s} Trace:{!s}]'.format(
                        record.threadName, record.name, record.funcName,
                        record.lineno, record.levelname, passed_traces)
                else:
                    prefix = '[{!s}.{!s}:{!s}:{!s} Trace:{!s}]'.format(
                        record.name, record.funcName, record.lineno,
                        record.levelname, passed_traces)
            text = '{} {}'.format(prefix, suffix)
        except Exception as e:
            pass

        return text