structlog_config#

Submodules#

Classes#

LoggerWithContext

A customized bound logger class that adds easy-to-remember methods for adding context.

Functions#

log_processors_for_mode(json_logger) → list[structlog.types.Processor]

Determine what the "final" processes in the pipeline should be to render the log to the output device.

get_default_processors(json_logger) → list[structlog.types.Processor]

Return the default list of log processors for structlog configuration.

add_simple_context_aliases(log) → LoggerWithContext

get_logger(*args, **kwargs) → LoggerWithContext

Get a structlog logger with the same context alias methods as the logger returned by configure_logger.

configure_logger(*[, json_logger, logger_factory, ...]) → LoggerWithContext

Create a structlog logger with some special additions:

Package Contents#

structlog_config.log_processors_for_mode(json_logger: bool) → list[structlog.types.Processor][source]#

Determine what the “final” processes in the pipeline should be to render the log to the output device.

  • If JSON, then structure exceptions as dicts and render as JSON

  • If not JSON, then use the ConsoleRenderer with a nice exception formatter.

structlog_config.get_default_processors(json_logger: bool) → list[structlog.types.Processor][source]#

Return the default list of log processors for structlog configuration.

This includes any “final” processors to render the log as json or not.

class structlog_config.LoggerWithContext[source]#

Bases: structlog.typing.FilteringBoundLogger, Protocol

A customized bound logger class that adds easy-to-remember methods for adding context.

We don’t use a real subclass because make_filtering_bound_logger has some logic we don’t want to replicate.

context(*args, **kwargs) → contextlib._GeneratorContextManager[None, None, None][source]#

context manager to temporarily set and clear logging context

local(*args, **kwargs) → None[source]#

set thread-local context

clear() → None[source]#

clear thread-local context

trace(*args, **kwargs) → None[source]#

trace level logging

structlog_config.add_simple_context_aliases(log) → LoggerWithContext[source]#
structlog_config.get_logger(*args, **kwargs) → LoggerWithContext[source]#

Get a structlog logger with the same context alias methods as the logger returned by configure_logger.

This is useful in cases where you want to get a logger without configuring it (e.g. in libraries or in tests).

structlog_config.configure_logger(*, json_logger: bool = False, logger_factory=None, install_exception_hook: bool = False, finalize_configuration: bool = False, enable_tee: bool = False) → LoggerWithContext[source]#

Create a structlog logger with some special additions:

>>> with log.context(key=value):
>>>    log.info("some message")
>>> log.local(key=value)
>>> log.info("some message")
>>> log.clear()
Parameters:
  • json_logger – Flag to use JSON logging. Defaults to False.

  • logger_factory – Optional logger factory to override the default.

  • install_exception_hook – Optional flag to install a global exception hook that logs uncaught exceptions using structlog. Defaults to False.

  • finalize_configuration – If True, any subsequent calls to configure_logger will be ignored with a warning. Useful to setup logging and globally and prevent accidental reconfiguration by other developers.

  • enable_tee – Flag to enable context-scoped log teeing support (tee_logs). Defaults to False.