Skip to content

pythonjsonlogger.json 🔗

JSON formatter using the standard library's json for encoding.

Module contains the JsonFormatter and a custom JsonEncoder which supports a greater variety of types.

CLASS DESCRIPTION
JsonEncoder

A custom encoder extending json.JSONEncoder

JsonFormatter

JSON formatter using the standard library's json for encoding

JsonEncoder 🔗

Bases: JSONEncoder

A custom encoder extending json.JSONEncoder

METHOD DESCRIPTION
format_datetime_obj

Format datetime objects found in self.default

format_datetime_obj 🔗

format_datetime_obj(o: time | date | datetime) -> str

Format datetime objects found in self.default

This allows subclasses to change the datetime format without understanding the internals of the default method.

JsonFormatter 🔗

JsonFormatter(
    *args,
    json_default: Optional[Callable] = None,
    json_encoder: Optional[Callable] = None,
    json_serializer: Callable = dumps,
    json_indent: Optional[Union[int, str]] = None,
    json_ensure_ascii: bool = True,
    **kwargs
)

Bases: BaseJsonFormatter

JSON formatter using the standard library's json for encoding

PARAMETER DESCRIPTION
args

DEFAULT: ()

json_default

a function for encoding non-standard objects

TYPE: Optional[Callable] DEFAULT: None

json_encoder

custom JSON encoder

TYPE: Optional[Callable] DEFAULT: None

json_serializer

a json.dumps-compatible callable that will be used to serialize the log record.

TYPE: Callable DEFAULT: dumps

json_indent

indent parameter for the json_serializer

TYPE: Optional[Union[int, str]] DEFAULT: None

json_ensure_ascii

ensure_ascii parameter for the json_serializer

TYPE: bool DEFAULT: True

kwargs

DEFAULT: {}

METHOD DESCRIPTION
add_fields

Extract fields from a LogRecord for logging

format

Formats a log record and serializes to json

formatException

Format and return the specified exception information.

formatStack

Format and return the specified stack information.

jsonify_log_record

Returns a json string of the log data.

parse

Parses format string looking for substitutions

process_log_record

Custom processing of the data to be logged.

serialize_log_record

Returns the final representation of the data to be logged

add_fields 🔗

add_fields(
    log_data: 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_data

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]

Changed in 4.0: log_record renamed to log_data

format 🔗

format(record: LogRecord) -> str

Formats a log record and serializes to json

PARAMETER DESCRIPTION
record

the record to format

TYPE: LogRecord

formatException 🔗

formatException(ei) -> Union[str, list[str]]

Format and return the specified exception information.

If exc_info_as_array is set to True, This method returns an array of strings.

formatStack 🔗

formatStack(stack_info) -> Union[str, list[str]]

Format and return the specified stack information.

If stack_info_as_array is set to True, This method returns an array of strings.

jsonify_log_record 🔗

jsonify_log_record(log_data: LogData) -> str

Returns a json string of the log data.

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_data: LogData) -> LogData

Custom processing of the data to be logged.

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

PARAMETER DESCRIPTION
log_data

incoming data

TYPE: LogData

Changed in 4.0: log_record renamed to log_data

serialize_log_record 🔗

serialize_log_record(log_data: LogData) -> str

Returns the final representation of the data to be logged

PARAMETER DESCRIPTION
log_data

the data

TYPE: LogData

Changed in 4.0: log_record renamed to log_data