def format(self, record: LogRecord) -> str: """ Format the specified record as text. The record's attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.get_message(). If the formatting string uses the time (as determined by a call to usesTime(), format_time() is called to format the event time. If there is exception information, it is formatted using format_exception() and appended to the message. """ record.message = record.get_message() if self._style.uses_time: record.asctime = self.format_time(record, self.datefmt) s = self.format_message(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.format_exception(record.exc_info) if record.exc_text: if s[-1:] != self.terminator: s = s + self.terminator s = s + record.exc_text if record.stack_info: if s[-1:] != self.terminator: s = s + self.terminator s = s + self.format_stack(record.stack_info) return s
def test_get_message_without_args(self): record = LogRecord( name="name", level=LogLevel.INFO, pathname=__file__, lineno=666, msg="Hello world!", ) self.assertEqual(record.get_message(), "Hello world!")
def test_get_message_with_args(self): record = LogRecord( name="name", level=LogLevel.INFO, pathname=__file__, lineno=666, msg="Dog: %(dog_name)s", args=({ "dog_name": "Xablau" }, ), ) self.assertEqual(record.get_message(), "Dog: Xablau")
def _get_msg_dict(self, record: LogRecord) -> Dict[str, str]: """ :param self: :param record: :return: """ record.message = record.get_message() record.asctime = self.format_time(record, self.datefmt) params = re.findall("%\((.*?)\)", self._fmt) _d = {} for i in params: _v = record.__dict__.get(i) if isinstance(_v, LogLevel): _v = _v.value _d[i] = str(_v) if _v else '' return _d