Skip to content

pythonjsonlogger.msgspec 🔗

JSON Formatter using msgspec

MsgspecFormatter 🔗

MsgspecFormatter(
    *args,
    json_default: OptionalCallableOrStr = msgspec_default,
    **kwargs
)

Bases: BaseJsonFormatter

JSON formatter using msgspec.json.Encoder for encoding.

PARAMETER DESCRIPTION
args

DEFAULT: ()

json_default

a function for encoding non-standard objects

TYPE: OptionalCallableOrStr DEFAULT: msgspec_default

kwargs

DEFAULT: {}

add_fields 🔗

add_fields(
    log_record: Dict[str, Any],
    record: LogRecord,
    message_dict: Dict[str, Any],
) -> None

Extract fields from a LogRecord for logging

This method can be overridden to implement custom logic for adding fields.

PARAMETER DESCRIPTION
log_record

data that will be logged

TYPE: Dict[str, Any]

record

the record to extract data from

TYPE: LogRecord

message_dict

dictionary that was logged instead of a message. e.g logger.info({"is_this_message_dict": True})

TYPE: Dict[str, Any]

format 🔗

format(record: LogRecord) -> str

Formats a log record and serializes to json

PARAMETER DESCRIPTION
record

the record to format

TYPE: LogRecord

jsonify_log_record 🔗

jsonify_log_record(log_record: LogRecord) -> str

Returns a json string of the log record.

parse 🔗

parse() -> List[str]

Parses format string looking for substitutions

This method is responsible for returning a list of fields (as strings) to include in all log messages.

You can support custom styles by overriding this method.

RETURNS DESCRIPTION
List[str]

list of fields to be extracted and serialized

process_log_record 🔗

process_log_record(log_record: LogRecord) -> LogRecord

Custom processing of the log record.

Child classes can override this method to alter the log record before it is serialized.

PARAMETER DESCRIPTION
log_record

incoming data

TYPE: LogRecord

serialize_log_record 🔗

serialize_log_record(log_record: LogRecord) -> str

Returns the final representation of the log record.

PARAMETER DESCRIPTION
log_record

the log record

TYPE: LogRecord

msgspec_default 🔗

msgspec_default(obj: Any) -> Any

msgspec default encoder function for non-standard types