Developer Referencertc
ConditionalExtraFormatter
Log formatter that appends selected extra fields when present.
Records logged with extra=\{'client_peer_id': ..., 'client_type': ...\}
get those fields appended as key=value pairs; records without them
format normally, so one formatter serves both peer-scoped and global
log lines.
Attributes
attributeextra_fieldsList[str]= extra_fields or ['client_peer_id', 'client_type']Functions
func__init__(self, fmt=None, datefmt=None, style='%', extra_fields=None) -> NoneSource Code
def __init__(self, fmt: Optional[str] = None, datefmt: Optional[str] = None,
style: str = '%', extra_fields: Optional[List[str]] = None) -> None:
super().__init__(fmt, datefmt, style)
self.extra_fields: List[str] = extra_fields or ['client_peer_id', 'client_type']paramselfparamfmtOptional[str]= NoneparamdatefmtOptional[str]= Noneparamstylestr= '%'paramextra_fieldsOptional[List[str]]= NoneReturns
Nonefuncformat(self, record) -> strFormat the record, appending any configured extra fields it carries.
Source Code
def format(self, record: logging.LogRecord) -> str:
"""Format the record, appending any configured extra fields it carries."""
result = super().format(record)
extra_parts = []
for field in self.extra_fields:
value = getattr(record, field, None)
if value is not None:
extra_parts.append(f"{field}={value}")
if extra_parts:
result = f"{result} | {' '.join(extra_parts)}"
return resultparamselfparamrecordlogging.LogRecordReturns
str