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.

JsonEncoder 🔗

Bases: JSONEncoder

A custom encoder extending json.JSONEncoder

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: OptionalCallableOrStr = None,
    json_encoder: OptionalCallableOrStr = None,
    json_serializer: Union[Callable, str] = json.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: OptionalCallableOrStr DEFAULT: None

json_encoder

custom JSON encoder

TYPE: OptionalCallableOrStr DEFAULT: None

json_serializer

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

TYPE: Union[Callable, str] 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: {}

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