예제 #1
0
    def format(self, record: logging.LogRecord):
        if isinstance(record.msg, dict):
            if hasattr(record, 'data'):
                record.data.update(record.msg)
            record.message = None
        else:
            record.message = record.getMessage()

        if record.message is not None and 'messageFormatted' not in self._skip_fields_calculation:
            if self.usesTime():
                record.asctime = self.formatTime(record, self.datefmt)
            record.messageFormatted = self.formatMessage(record)

        if record.exc_info:
            record.exceptionClass = record.exc_info[0].__name__
            record.exceptionMessage = str(record.exc_info[1])
            # Cache the traceback text to avoid converting it multiple times (it's constant anyway)
            if not record.exc_text and 'exc_text' not in self._skip_fields_calculation:
                record.exc_text = self.formatException(record.exc_info)

        json_data = record.__dict__

        del json_data['msg'], json_data['args']
        # We can't serialize exc_info to JSON by default thus drop it.
        del json_data['exc_info']

        for field in self._drop_fields_from_json:
            if field in json_data:
                del json_data[field]

        return json.dumps(json_data, **self._json_dumps_args)