MCPcopy Create free account
hub / github.com/hynek/structlog / format

Method format

src/structlog/stdlib.py:1136–1216  ·  view source on GitHub ↗

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.

(self, record: logging.LogRecord)

Source from the content-addressed store, hash-verified

1134 self.use_get_message = use_get_message
1135
1136 def format(self, record: logging.LogRecord) -> str:
1137 """
1138 Extract *structlog*'s `event_dict` from ``record.msg`` and format it.
1139
1140 *record* has been patched by `wrap_for_formatter` first though, so the
1141 type isn't quite right.
1142 """
1143 # Make a shallow copy of the record to let other handlers/formatters
1144 # process the original one
1145 record = logging.makeLogRecord(record.__dict__)
1146
1147 logger = getattr(record, "_logger", _SENTINEL)
1148 meth_name = getattr(record, "_name", "__structlog_sentinel__")
1149
1150 ed: ProcessorReturnValue
1151 if logger is not _SENTINEL and meth_name != "__structlog_sentinel__":
1152 # Both attached by wrap_for_formatter
1153 if self.logger is not None:
1154 logger = self.logger
1155 meth_name = cast(str, record._name) # type:ignore[attr-defined]
1156
1157 # We need to copy because it's possible that the same record gets
1158 # processed by multiple logging formatters. LogRecord.getMessage
1159 # would transform our dict into a str.
1160 ed = cast(dict[str, Any], record.msg).copy()
1161 ed["_record"] = record
1162 ed["_from_structlog"] = True
1163 else:
1164 logger = self.logger
1165 meth_name = record.levelname.lower()
1166 ed = {
1167 "event": (
1168 record.getMessage()
1169 if self.use_get_message
1170 else str(record.msg)
1171 ),
1172 "_record": record,
1173 "_from_structlog": False,
1174 }
1175
1176 if self.pass_foreign_args:
1177 ed["positional_args"] = record.args
1178
1179 record.args = ()
1180
1181 # Add stack-related attributes to the event dict
1182 if record.exc_info:
1183 ed["exc_info"] = record.exc_info
1184 if record.stack_info:
1185 ed["stack_info"] = record.stack_info
1186
1187 # Non-structlog allows to run through a chain to prepare it for the
1188 # final processor (e.g. adding timestamps and log levels).
1189 for proc in self.foreign_pre_chain or ():
1190 ed = cast(EventDict, proc(logger, meth_name, ed))
1191
1192 # If required, unset stack-related attributes on the record copy so
1193 # that the base implementation doesn't append stacktraces to the

Callers 2

_init_terminalFunction · 0.80

Calls 1

warnMethod · 0.45

Tested by 1