def format(self, record: logging.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.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message. """ if record.levelno == logging.DEBUG: self._fmt = self.dbg_fmt elif record.levelno == logging.INFO: self._fmt = self.info_fmt else: self._fmt = self.default_fmt self._style = logging.PercentStyle(self._fmt) record.message = 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) return s
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)
def format(self, record: logging.LogRecord): # Change it so we save the initial string (without extras), and are not reliant on implementation record.message = record.getMessage() msg = record.message if self.usesTime(): record.asctime = self.formatTime(record, self.datefmt) initial_str = self.formatMessage(record) s = initial_str 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) # Replace newlines with indentation header_length = 0 if initial_str.endswith(msg): header_length = len(initial_str) - len(msg) replace_str = '\n' + ' ' * header_length if header_length >= 2: replace_str = replace_str[:-2] + '| ' s = s.replace('\n', replace_str) return s
def format(self, record: logging.LogRecord) -> str: """ Internal function to format the :class:`LogRecord` as HTML. See 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=f";background-color:{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
def emit(self, record: LogRecord) -> None: """Invoked by logging.""" message = self.format(record) traceback = None if (self.rich_tracebacks and record.exc_info and record.exc_info != (None, None, None)): exc_type, exc_value, exc_traceback = record.exc_info assert exc_type is not None assert exc_value is not None traceback = Traceback.from_exception( exc_type, exc_value, exc_traceback, width=self.tracebacks_width, extra_lines=self.tracebacks_extra_lines, theme=self.tracebacks_theme, word_wrap=self.tracebacks_word_wrap, show_locals=self.tracebacks_show_locals, locals_max_length=self.locals_max_length, locals_max_string=self.locals_max_string, ) message = record.getMessage() if self.formatter: record.message = record.getMessage() formatter = self.formatter if hasattr(formatter, "usesTime") and formatter.usesTime(): record.asctime = formatter.formatTime( record, formatter.datefmt) message = formatter.formatMessage(record) message_renderable = self.render_message(record, message) log_renderable = self.render(record=record, traceback=traceback, message_renderable=message_renderable) self.console.print(log_renderable)
def format(self, record: LogRecord) -> str: args = record.args if args == ( {}, ): # logger.info('message', {}) -> record.args == ({},) args = {} assert isinstance( args, Mapping ), "monitor record argument must be a mapping, not %r" % type(args) assert all( elem is not None for elem in args.values()), "line protocol values must not be None" record.asctime = self.formatTime(record, self.datefmt) record.message = record.getMessage() if args else record.msg tags = self._default_tags.copy() tags.update({ key: value for key, value in args.items() if key in self._tags_whitelist }) fields = { key: value for key, value in args.items() if key not in self._fields_blacklist } timestamp = (record.created // self._resolution * self._resolution if self._resolution is not None else None) return line_protocol(name=record.message, tags=tags, fields=fields, timestamp=timestamp)
def format(self, record: LogRecord) -> Dict[str, str]: record.message = record.getMessage() record.levelname_lower = record.levelname.lower() # in the case that no formatter have been provided return an empty dictionary if len(self._formatters) == 0: return dict() # use one of the formatter to set some attribute in the record formatter = self._formatters[list(self._formatters.keys())[0]] if self._uses_time: record.asctime = formatter.formatTime(record=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 = formatter.formatException(record.exc_info) key_values_message = self._format_message(record=record) key_values_exception = self._format_exception(record=record) # merge the key-values of the message and the exception such that the message key-values overwrite the # exception key-values incase of duplicates return {**key_values_exception, **key_values_message}
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
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
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
def format(self, record: LogRecord) -> str: """Format the specified record as text.""" if 'asctime' in settings.LOGGING_FIELDS: record.asctime = self.formatTime(record, self.datefmt) record = self.add_user(record) message = record.getMessage() dict_record = self.to_dict(message, record) return self.serializer.to_json(dict_record)
def format(self, record: logging.LogRecord) -> str: record.asctime = datetime.datetime.fromtimestamp( record.created).isoformat() # This will prevent any ANSI code from making their way to # the log files msg = re.sub("\\033\[[0-9;]+m", "", record.getMessage()) # noqa: W605 res = f"[{record.asctime}] [{record.levelname}] {record.name}: {msg}" if record.levelno >= logging.ERROR and record.exc_info is not None: res += "\n" + "\n".join(format_exception(*record.exc_info)) + "\n" return res
def format(self, record: LogRecord) -> str: record.asctime = datetime.datetime.now().isoformat() self.writer.writerow( [record.asctime, record.levelname, record.name, record.msg]) data = self.output.getvalue() self.output.truncate(0) self.output.seek(0) return data.strip()
def format(self, record: logging.LogRecord) -> str: record.funcName: str = html.escape(record.funcName) record.name: str = html.escape(record.name) record.msg: str = html.escape(str(record.msg)) record.message = record.getMessage() if self.usesTime(): record.asctime = self.formatTime(record, self.datefmt) record.exc_text = str() if record.exc_info: record.exc_text = self.formatException(record.exc_info) return self.formatMessage(record)
def format(self, record: LogRecord) -> str: """ Format a LogRecord as a string. :param record: the record to format :return: the formatted string """ record.asctime = datetime.datetime.utcnow().strftime( "%Y-%m-%dT%H:%M:%S.%fZ") message = record.getMessage() if record.exc_info: eno = record.exc_info stacktrace = "".join( traceback.format_exception(None, eno[1], eno[2])) message += f" excp: {stacktrace}" if record.stack_info: stack = self.formatStack(record.stack_info) message += f" trace: {stack}" log_message = EnoLogMessage( tool=type(self.checker).__name__, type="infrastructure", severity=record.levelname, severity_level=max(0, record.levelno // 10 - 1), timestamp=record.asctime, message=message, module=record.module, function=record.funcName, service_name=self.checker.service_name, method=self.checker.method.value if self.checker.method else None, task_id=self.checker.task_id, team_id=self.checker.team_id, team_name=self.checker.team_name, current_round_id=self.checker.current_round_id, related_round_id=self.checker.related_round_id, flag=self.checker.flag, variant_id=self.checker.variant_id, task_chain_id=self.checker.task_chain_id, flag_regex=self.checker.flag_regex, flag_hash=self.checker.flag_hash, attack_info=self.checker.attack_info, ) return LOGGING_PREFIX + jsons.dumps( log_message, use_enum_name=False, key_transformer=jsons.KEY_TRANSFORMER_CAMELCASE, strict=True, )
def format(self, record: LogRecord) -> str: record.message = re.sub(RICH_FORMAT_REGEX, "", 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" 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
def format(self, record: LogRecord) -> str: """ Format a LogRecord as a string. :param record: the record to format :return: the formatted string """ record.asctime = datetime.datetime.utcnow().strftime( "%Y-%m-%dT%H:%M:%S.%fZ") message = record.getMessage() if record.exc_info: eno = record.exc_info stacktrace = "".join( traceback.format_exception(None, eno[1], eno[2])) message += f" excp: {stacktrace}" if record.stack_info: stack = self.formatStack(record.stack_info) message += f" trace: {stack}" log_output = { "tool": type(self.checker).__name__, "type": "infrastructure", "severity": record.levelname, "severityLevel": max(0, record.levelno // 10 - 1), "timestamp": record.asctime, "module": record.module, "function": record.funcName, "flag": self.checker.flag, "flagIndex": self.checker.flag_idx, "runId": self.checker.run_id, "roundId": self.checker.round, "relatedRoundId": self.checker.flag_round, "message": message, "teamName": self.checker.team, "teamId": self.checker.team_id, "serviceName": self.checker.service_name, "method": self.checker.method, } return LOGGING_PREFIX + json.dumps(log_output)
def format(self, record: logging.LogRecord) -> str: """Return formatted string of log record. Args: record: The log record. Returns: Formatted string of log record. """ try: record.asctime except AttributeError: record.asctime = self.formatTime(record, self.datefmt) try: record.message except AttributeError: record.message = record.msg s = '{} - {:>8} - {:>26} - {}'.format(record.asctime, record.levelname, record.name, record.message) return s
def _formatTime(self, record: logging.LogRecord) -> None: if self.usesTime(): record.asctime = self.formatTime(record, self.datefmt)
def format(self, record: logging.LogRecord): record.message = record.getMessage() if self.usesTime(): record.asctime = self.formatTime(record, self.datefmt) return self.formatMessage(record)
def format(self, record: LogRecord): record.asctime = Color.CYAN(self.formatTime(record, self.datefmt)) record.msg = Color.GREY(record.msg) record.name = Color.PURPLE(record.name) record.levelname = self.COLORS[record.levelname](record.levelname) return super().format(record)