Logging API Reference¶
configure_logging, get_logger, and JSON formatting helpers.
Logging for Istos.
Istos is a library: importing it must not reconfigure the root logger or
steal an application's log output. Following the standard-library convention, the
istos logger only ever emits records and carries a :class:~logging.NullHandler
so nothing is printed and no "No handlers could be found" warning is raised. All
output configuration (handlers, formatters, level, text vs JSON) is left to the
embedding application.
Applications that want Istos to configure output can opt in with
:func:configure_logging. Istos.run() calls :func:ensure_configured, which
installs a default handler only when neither Istos nor the app has
already configured one — so a standalone service still prints, while a service
embedded in a larger app that manages logging is left untouched.
Usage in framework code::
from istos.logging import get_logger
logger = get_logger("handler") # -> logging.getLogger("istos.handler")
logger.info("Bound handler %s", prefix, extra={"prefix": prefix})
Messages are human-readable sentences with lazy % args; structured context
travels in a flat extra= dict and is surfaced by the JSON formatter.
ColorFormatter
¶
Bases: Formatter
Human-readable console formatter that colourises the level name (dev).
Source code in src/istos/logging.py
StructuredFormatter
¶
Bases: Formatter
JSON log formatter for production aggregation.
Lifts any extra fields — whether passed flat via extra={...} or in the
legacy extra_fields envelope — onto the top-level JSON object.
Source code in src/istos/logging.py
configure_logging(level='INFO', json_format=False, *, color=None, logger_name=_ROOT_LOGGER_NAME)
¶
Attach an output handler to the Istos logger (opt-in).
Libraries embedding Istos should generally NOT call this and instead
configure their own root logger; Istos records will propagate to it. Call
this only for a standalone service, or pass Istos(configure_logging=True).
color defaults to auto (on when stderr is a TTY and json_format is
False).
Source code in src/istos/logging.py
ensure_configured(level='INFO', json_format=False)
¶
Install a default handler only if nothing else has configured logging.
Used by Istos.run() so a standalone service prints logs, without
clobbering an application that already manages its own logging.
Source code in src/istos/logging.py
get_logger(name=None)
¶
Return a logger under the istos namespace.
Does not configure output — that is the application's responsibility (see
:func:configure_logging).
Source code in src/istos/logging.py
log_with_context(logger, level, message, **fields)
¶
Emit a log record with structured extra fields (flat extra=).