def makePickle(self, record: LogRecord) -> str: """Pickle the log record. Pickles the record in binary format with a length prefix, and returns it ready for transmission across the socket. """ ei = record.exc_info if ei: _ = self.format( record) # just to get traceback text into record.exc_text record.exc_info = None # to avoid Unpickleable error d = dict(record.__dict__) s = json.dumps(d) if ei: record.exc_info = ei # for next handler return s
def format(self, record: logging.LogRecord) -> str: """ Extract ``structlog``'s `event_dict` from ``record.msg`` and format it. *record* has been patched by `wrap_for_formatter` first though, so the type isn't quite right. """ # Make a shallow copy of the record to let other handlers/formatters # process the original one record = logging.makeLogRecord(record.__dict__) logger = getattr(record, "_logger", _SENTINEL) meth_name = getattr(record, "_name", _SENTINEL) if logger is not _SENTINEL and meth_name is not _SENTINEL: # Both attached by wrap_for_formatter if self.logger is not None: logger = self.logger meth_name = record._name # type: ignore # We need to copy because it's possible that the same record gets # processed by multiple logging formatters. LogRecord.getMessage # would transform our dict into a str. ed = record.msg.copy() # type: ignore else: logger = self.logger meth_name = record.levelname.lower() ed = {"event": record.getMessage(), "_record": record} if self.pass_foreign_args: ed["positional_args"] = record.args record.args = () # Add stack-related attributes to event_dict and unset them # on the record copy so that the base implementation wouldn't # append stacktraces to the output. if record.exc_info: ed["exc_info"] = record.exc_info if record.stack_info: ed["stack_info"] = record.stack_info if not self.keep_exc_info: record.exc_text = None record.exc_info = None if not self.keep_stack_info: record.stack_info = None # Non-structlog allows to run through a chain to prepare it for the # final processor (e.g. adding timestamps and log levels). for proc in self.foreign_pre_chain or (): ed = proc(logger, meth_name, ed) del ed["_record"] record.msg = self.processor(logger, meth_name, ed) # type: ignore return super().format(record)
def emit(self, record: logging.LogRecord) -> None: """ Records the log message. """ # Remove `exc_info` to reclaim memory. if record.exc_info: if not record.exc_text: record.exc_text = ''.join(format_exception(*record.exc_info)) record.exc_info = None self._records.append(record) self.max_level_emitted = max(self.max_level_emitted, record.levelno)
def _format_record(self, record: logging.LogRecord=None) -> logging.LogRecord: """ Formatted the LogRecord :param record: LogRecord with the message info :return: LogRecord with formatted message """ # ensure that exc_info and args # have been stringified. Removes any chance of # unpickleable things inside and possibly reduces # message size sent over the pipe. if record.args: record.msg = record.msg % record.args record.args = None if record.exc_info: self.format(record) record.exc_info = None return record
def _format_record(self, record: logging.LogRecord = None) -> logging.LogRecord: """ Formatted the LogRecord :param record: LogRecord with the message info :return: LogRecord with formatted message """ # ensure that exc_info and args # have been stringified. Removes any chance of # unpickleable things inside and possibly reduces # message size sent over the pipe. if record.args: record.msg = record.msg % record.args record.args = None if record.exc_info: self.format(record) record.exc_info = None return record
def filter(record: logging.LogRecord) -> bool: """Removes exception stack traces information.""" record.exc_info = None record.exc_text = None # type: ignore return True