def format(self, record):
        stdout_template = '{levelname}' + Fore.RESET + '] {threadName}: ' + '{message}'
        stdout_head = '[%s'

        allFormats = {
            logging.DEBUG:
            logging.StrFormatStyle(stdout_head % Fore.LIGHTBLUE_EX +
                                   stdout_template),
            logging.INFO:
            logging.StrFormatStyle(stdout_head % Fore.GREEN + stdout_template),
            logging.WARNING:
            logging.StrFormatStyle(stdout_head % Fore.LIGHTYELLOW_EX +
                                   stdout_template),
            logging.ERROR:
            logging.StrFormatStyle(stdout_head % Fore.LIGHTRED_EX +
                                   stdout_template),
            logging.CRITICAL:
            logging.StrFormatStyle(stdout_head % Fore.RED + stdout_template)
        }

        self._style = allFormats.get(
            record.levelno, logging.StrFormatStyle(logging._STYLES['{'][1]))
        self._fmt = self._style._fmt
        result = logging.Formatter.format(self, record)

        return result
示例#2
0
    def format(self, record):  # noqa
        """Set the format of the log."""
        fmt = "{asctime} | {levelname} |"

        if self._mylogger.level <= logging.DEBUG:
            fmt += " {filename}::{funcName}() |"

        if current_process().name != "MainProcess":
            fmt += " pid={process} |"

        self._style = logging.StrFormatStyle(fmt + " {message}")

        return logging.Formatter.format(self, record)
示例#3
0
    def test_log_strformat_style_format(self):
        # DEV: We have to use `{msg}` instead of `{message}` because we are manually creating
        # records which does not properly configure `record.message` attribute
        fmt = (
            "{msg} [dd.service={dd.service} dd.env={dd.env} "
            "dd.version={dd.version} dd.trace_id={dd.trace_id} dd.span_id={dd.span_id}]"
        )
        formatter = logging.StrFormatStyle(fmt)

        with self.override_config("logging", dict(tracer=self.tracer)):
            with self.tracer.trace("test.logging") as span:
                record = logger.makeRecord("name", "INFO", "func", 534,
                                           "Manual log record", (), None)
                log = formatter.format(record)
                expected = "Manual log record [dd.service= dd.env= dd.version= dd.trace_id={} dd.span_id={}]".format(
                    span.trace_id, span.span_id)
                assert log == expected

                assert not hasattr(record, "dd")
                assert getattr(record,
                               RECORD_ATTR_TRACE_ID) == str(span.trace_id)
                assert getattr(record,
                               RECORD_ATTR_SPAN_ID) == str(span.span_id)
示例#4
0
 def format(self, record):
     self._fmt = self.FORMATS.get(record.levelno, self.FORMATS['default'])
     self._style = logging.StrFormatStyle(self._fmt)
     return super().format(record)