pythonjsonlogger.core
๐
Core functionality shared by all JSON loggers
OptionalCallableOrStr
module-attribute
๐
Type alias
RESERVED_ATTRS
module-attribute
๐
RESERVED_ATTRS: List[str] = [
"args",
"asctime",
"created",
"exc_info",
"exc_text",
"filename",
"funcName",
"levelname",
"levelno",
"lineno",
"module",
"msecs",
"message",
"msg",
"name",
"pathname",
"process",
"processName",
"relativeCreated",
"stack_info",
"thread",
"threadName",
]
Default reserved attributes.
These come from the default attributes of LogRecord
objects.
Note
Although considered a constant, this list is dependent on the Python version due to
different LogRecord
objects having different attributes in different Python versions.
Changed in 3.0: RESERVED_ATTRS
is now list[str]
instead of tuple[str, ...]
.
BaseJsonFormatter
๐
BaseJsonFormatter(
fmt: Optional[str] = None,
datefmt: Optional[str] = None,
style: str = "%",
validate: bool = True,
*,
prefix: str = "",
rename_fields: Optional[Dict[str, str]] = None,
rename_fields_keep_missing: bool = False,
static_fields: Optional[Dict[str, Any]] = None,
reserved_attrs: Optional[Sequence[str]] = None,
timestamp: Union[bool, str] = False,
defaults: Optional[Dict[str, Any]] = None
)
Bases: Formatter
Base class for all formatters
Must not be used directly.
New in 3.1
PARAMETER | DESCRIPTION |
---|---|
fmt |
string representing fields to log |
datefmt |
format to use when formatting |
style |
how to extract log fields from
TYPE:
|
validate |
validate
TYPE:
|
defaults |
ignored - kept for compatibility with python 3.10+ |
prefix |
an optional string prefix added at the beginning of the formatted string
TYPE:
|
rename_fields |
an optional dict, used to rename field names in the output.
Rename |
rename_fields_keep_missing |
When renaming fields, include missing fields in the output.
TYPE:
|
static_fields |
an optional dict, used to add fields with static values to all logs |
reserved_attrs |
an optional list of fields that will be skipped when outputting json log record. Defaults to all log record attributes. |
timestamp |
an optional string/boolean field to add a timestamp when outputting the json log record. If string is passed, timestamp will be added to log record using string as key. If True boolean is passed, timestamp key will be "timestamp". Defaults to False/off. |
Changed in 3.1:
- you can now use custom values for style by setting validate to
False
. The value is stored inself._style
as a string. Theparse
method will need to be overridden in order to support the new style. - Renaming fields now preserves the order that fields were added in and avoids adding
missing fields. The original behaviour, missing fields have a value of
None
, is still available by settingrename_fields_keep_missing
toTrue
.
add_fields
๐
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 |
record |
the record to extract data from
TYPE:
|
message_dict |
dictionary that was logged instead of a message. e.g
|
format
๐
Formats a log record and serializes to json
PARAMETER | DESCRIPTION |
---|---|
record |
the record to format
TYPE:
|
jsonify_log_record
๐
Convert this log record into a JSON string.
Child classes MUST override this method.
PARAMETER | DESCRIPTION |
---|---|
log_record |
the data to serialize
TYPE:
|
parse
๐
merge_record_extra
๐
merge_record_extra(
record: LogRecord,
target: Dict,
reserved: Container[str],
rename_fields: Optional[Dict[str, str]] = None,
) -> Dict
Merges extra attributes from LogRecord object into target dictionary
PARAMETER | DESCRIPTION |
---|---|
record |
logging.LogRecord
TYPE:
|
target |
dict to update
TYPE:
|
reserved |
dict or list with reserved keys to skip |
rename_fields |
an optional dict, used to rename field names in the output.
e.g. Rename |
Changed in 3.1: reserved
is now Container[str]
.