Skip to content

pythonjsonlogger.orjson 🔗

JSON Formatter using orjson

OrjsonFormatter 🔗

OrjsonFormatter(
    *args,
    json_default: OptionalCallableOrStr = orjson_default,
    json_indent: bool = False,
    **kwargs
)

Bases: BaseJsonFormatter

JSON formatter using orjson for encoding.

PARAMETER DESCRIPTION
args

DEFAULT: ()

json_default

a function for encoding non-standard objects

TYPE: OptionalCallableOrStr DEFAULT: orjson_default

json_indent

indent output with 2 spaces.

TYPE: bool DEFAULT: False

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

orjson_default 🔗

orjson_default(obj: Any) -> Any

orjson default encoder function for non-standard types