Пример #1
0
    def add_fields(self, log_record, record, message_dict):
        """
        This method allows us to inject custom data into resulting log messages
        """
        for field in self._required_fields:
            log_record[field] = record.__dict__.get(field)
        log_record.update(message_dict)

        # Add timestamp and application name if not present
        if "timestamp" not in log_record:
            now = datetime.datetime.now()
            log_record["timestamp"] = datetime.datetime.strftime(
                now, format=constants.LOGGING_TS_FORMAT)

        if "application" not in log_record:
            log_record["application"] = constants.APPLICATION_NAME

        for field in log_record:
            if log_record[field] is not None:
                log_record[field] = str(log_record[field])
            else:
                log_record[field] = ""

        # message field returns null every time so remove it, if exists
        log_record.pop("message", None)
        jsonlogger.merge_record_extra(record,
                                      log_record,
                                      reserved=self._skip_fields)
Пример #2
0
 def add_fields(self, log_record, record, message_dict):
     log_record['timestamp'] = datetime.utcnow().strftime(
         '%Y-%m-%dT%H:%M:%S.%fZ')
     log_record['level'] = record.levelname
     log_record['message'] = record.message
     merge_record_extra(record, log_record, reserved=self._skip_fields)
     if log_record['level'] == 'ERROR':
         log_record['nested_error'] = message_dict['exc_info']
Пример #3
0
    def add_fields(self, log_record: dict, record: LogRecord, message_dict: dict) -> None:
        """
        Adds additional log information from `log_record` to `records. If a required field does not exist in the
        `log_record` then it is not included in the `record`.

        :param log_record: additional fields to add to the `record`.
        :param record: the logRecord to add additional fields too.
        :param message_dict: the log message and extra fields to add to `records`.
        :return:
        """
        for field in self._required_fields:
            value = record.__dict__.get(field)
            if value:
                log_record[field] = value
        log_record.update(message_dict)
        merge_record_extra(record, log_record, reserved=self._skip_fields)
Пример #4
0
    def add_fields(self, log_record, record, message_dict):
        """
        This method allows us to inject custom data into resulting log messages
        """
        for field in self._required_fields:
            log_record[field] = record.__dict__.get(field)
        log_record.update(message_dict)

        # Add timestamp and application name if not present
        if "timestamp" not in log_record:
            log_record["timestamp"] = datetime.datetime.utcnow().isoformat()
        if "application" not in log_record:
            log_record["application"] = self.conf.app_name
        if "version" not in log_record:
            log_record["version"] = self.conf.version

        jsonlogger.merge_record_extra(record,
                                      log_record,
                                      reserved=self._skip_fields)
Пример #5
0
    def format(self, record):
        # add color
        if self.colors and record.levelname in COLORS:
            start = COLOR_SEQ.format(COLORS[record.levelname])
            record.levelname = start + record.levelname + RESET_SEQ
            record.msg = COLOR_SEQ.format(WHITE) + record.msg + RESET_SEQ

        # add extras
        if self.extras:
            extras = merge_record_extra(record=record,
                                        target=dict(),
                                        reserved=RESERVED_ATTRS)
            record.extras = ', '.join('{}={}'.format(k, v)
                                      for k, v in extras.items())
            if record.extras:
                record.extras = COLOR_SEQ.format(MAGENTA) + '({})'.format(
                    record.extras) + RESET_SEQ

        return super().format(record)
Пример #6
0
    def format(self, record):
        message = record.getMessage()

        extra = merge_record_extra(record, dict())
        if not isinstance(record.msg, dict) and extra:
            message = self.format_safely(message, **extra)

        if self.format_string.find("%(asctime)") >= 0:
            record.asctime = self.formatTime(record, self.datefmt)

        log_string = self.format_safely(
            self.format_string,
            asctime=self.formatTime(record),
            name=record.name,
            levelname=record.levelname,
            message=message
        )

        if record.exc_info:
            if log_string[-1] != "\n":
                log_string = log_string + "\n"
            log_string = log_string + self.formatException(record.exc_info)

        return log_string
    def format(self, record):
        message = record.getMessage()

        extra = merge_record_extra(record=record, target=dict(), reserved=dict())
        if not isinstance(record.msg, dict) and extra:
            message = self.format_safely(message, **extra)

        if self.format_string.find("%(asctime)") >= 0:
            record.asctime = self.formatTime(record, self.datefmt)

        log_string = self.format_safely(
            self.format_string,
            asctime=self.formatTime(record),
            name=record.name,
            levelname=record.levelname,
            message=message
        )

        if record.exc_info:
            if log_string[-1] != "\n":
                log_string = log_string + "\n"
            log_string = log_string + self.formatException(record.exc_info)

        return log_string