logger
logger
¶
Structured logging with key=value and JSON output support.
Wraps the standard library logging module to provide structured output
in two formats: human-readable key=value pairs (default) and machine-parseable
JSON for production/cloud environments.
Values containing spaces, equals signs, or quotes are automatically escaped and wrapped in double quotes. Long values are truncated to a configurable maximum length.
The StructuredFormatter is a
stdlib logging.Formatter that reads structured data from the
structured_kv extra field (attached by
Logger) and appends it as key=value pairs.
When installed on the root handler, it unifies output from both
Logger and plain logging.getLogger()
calls used in the models and utils layers.
Examples:
from bigbrotr.core.logger import Logger
logger = Logger("finder")
logger.info("started", cycle=1, count=42)
# Output: started cycle=1 count=42
json_logger = Logger("finder", json_output=True)
json_logger.info("started", cycle=1)
# Output: {"message": "started", "cycle": 1}
See Also
BaseService: Creates a
Logger instance for each service
using SERVICE_NAME.
format_kv_pairs(): Standalone
formatting utility used by both
Logger and
StructuredFormatter.
Classes¶
StructuredFormatter
¶
Bases: Formatter
Formats all log records as structured key=value output.
Reads structured data from the structured_kv extra field
(attached by Logger) and appends it
as key=value pairs via
format_kv_pairs(). When no
structured_kv is present (e.g. plain logging.getLogger()
calls from models/utils), the message is emitted as-is with the
same level name message prefix for consistency.
See Also
Logger: Attaches the
structured_kv extra field to log records.
format_kv_pairs():
Formatting utility used by the format() method.
Logger
¶
Structured logger that appends keyword arguments as extra fields.
Wraps a standard logging.Logger and formats keyword arguments as either
key=value pairs or JSON, depending on configuration. All public methods
mirror the standard logging API with an added **kwargs parameter.
Examples:
logger = Logger("finder")
logger.info("cycle_completed", cycle=1, duration=2.5)
# Output: cycle_completed cycle=1 duration=2.5
logger.info("relay_found", url="wss://relay.example.com")
# Output: relay_found url=wss://relay.example.com
Note
This class intentionally does not use __slots__. Adding
__slots__ would break unittest.mock.patch.object() which
needs to set arbitrary attributes on the instance during testing.
See Also
StructuredFormatter:
Stdlib formatter that reads the structured_kv extra field
attached by this class.
format_kv_pairs(): Standalone
formatting utility.
BaseService: Creates a Logger
instance per service via Logger(SERVICE_NAME).
Initialize a structured logger.
Parameters:
-
name(str) –Logger name, typically the service or module name. Maps to the underlying
logging.getLogger(name)call. -
json_output(bool, default:False) –If
True, emit JSON objects instead of key=value pairs. JSON output includestimestamp(ISO 8601),level, andservicefields for compatibility with log aggregators. -
max_value_length(int | None, default:None) –Maximum character length for individual values before truncation. Defaults to
1000.
Source code in src/bigbrotr/core/logger.py
Functions¶
debug
¶
Log a DEBUG level message with optional key=value pairs.
Source code in src/bigbrotr/core/logger.py
info
¶
Log an INFO level message with optional key=value pairs.
Source code in src/bigbrotr/core/logger.py
warning
¶
Log a WARNING level message with optional key=value pairs.
Source code in src/bigbrotr/core/logger.py
error
¶
Log an ERROR level message with optional key=value pairs.
Source code in src/bigbrotr/core/logger.py
critical
¶
Log a CRITICAL level message with optional key=value pairs.
Source code in src/bigbrotr/core/logger.py
exception
¶
Log an ERROR level message with exception traceback and optional key=value pairs.
Source code in src/bigbrotr/core/logger.py
Functions¶
format_kv_pairs
¶
format_kv_pairs(
kwargs: dict[str, Any],
max_value_length: int | None = 1000,
prefix: str = " ",
) -> str
Format a dictionary as space-separated key=value pairs.
Used by Logger and
StructuredFormatter to
produce consistent structured output. Values are truncated to
max_value_length characters, and values containing whitespace,
equals signs, or quotes are automatically escaped and quoted.
Parameters:
-
kwargs(dict[str, Any]) –Key-value pairs to format.
-
max_value_length(int | None, default:1000) –Maximum characters per value before truncation. Pass
Noneto disable truncation. -
prefix(str, default:' ') –String prepended to the output (default: single space).
Returns:
-
str–Formatted string, e.g.
' key1=value1 key2="value with spaces"'. -
str–Returns empty string if
kwargsis empty.
See Also
Logger: Primary consumer of this function.