Logging

Logging module for Peak SDK.

class peak.tools.logging.LogHandler(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Enumeration of log handlers to be used in logging.

Each enum member corresponds to a specific handler defined in the logging module. This enum provides a convenient way to specify handlers when configuring loggers.

CONSOLE

Represents a console handler, intended for displaying logs in the console.

FILE

Represents a file handler, intended for writing logs to a file.

class peak.tools.logging.LogLevel(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Enumeration of log levels to be used in logging.

Each enum member corresponds to a specific log level defined in the logging module. The enum provides a convenient way to specify log levels when configuring loggers.

DEBUG

Debug log level. Intended for detailed debugging information.

INFO

Info log level. Used for general information about program execution.

WARN

Warning log level. Indicates potential issues or unexpected behavior.

WARNING

Warning log level. Indicates potential issues or unexpected behavior.

ERROR

Error log level. Indicates errors that do not prevent program execution.

EXCEPTION

Error log level. Indicates errors that do not prevent program execution.

CRITICAL

Critical log level. Indicates severe errors that might lead to program failure.

FATAL

Critical log level. Indicates severe errors that might lead to program failure.

class peak.tools.logging.PeakLogger(logger)

Wrapper class for logging with various log levels.

Parameters:

logger (Any) –

bind(context=None, **kwargs)

Bind contextual information to the logger, enriching log messages.

This method allows attaching context data to the logger, such as additional information or system details, to provide more context in log messages.

Parameters:
  • context (Union[dict[str, Any], None]) – A dictionary or None for contextual information.

  • **kwargs – Additional key-value pairs to enhance context.

Return type:

None

clone_with_context(context=None, **kwargs)

Return a frozen copy of this logger with the specified context added.

Parameters:
  • context (dict[str, Any] | None) –

  • kwargs (Any) –

Return type:

PeakLogger

set_log_level(level)

Set the log level of the root logger.

Parameters:

level (LogLevel) – Log level to set.

Return type:

None

unbind(keys)

Unbind specified keys from the logger’s context.

Parameters:

keys (list[str]) – List of keys to unbind.

Return type:

None

peak.tools.logging.default_processors_factory(disable_masking)

Return the default processors for PeakLogger.

Parameters:

disable_masking (Optional[bool], optional) – Whether to disable masking of sensitive data. Defaults to False.

Returns:

List of processors to be used by the logger.

Return type:

list[structlog.types.Processor | Any]

peak.tools.logging.get_logger(name=None, level=LogLevel.INFO, custom_processors_factory=None, disable_masking=False, handlers=None, file_name=None, **kwargs)

Return a logger with the specified settings.

When using the default implementation, pretty-printing is automatically enabled when logger is run in a terminal session (sys.stdout.isatty() == True) and JSON printing is enabled when logger is run in production via the structlog.processors.JSONRenderer processor.

Parameters:
  • name (Optional[str], optional) – Name of the logger. Defaults to None.

  • level (LogLevel) – Log level. Defaults to LogLevel.INFO.

  • custom_processors_factory (Optional[Callable[..., List[structlog.types.Processor | Any]]], optional) – A factory function that returns a list of custom processors. Defaults to None. This disables the default processors provided with the default implementation.

  • disable_masking (Optional[bool], optional) – Whether to disable masking of sensitive data. Defaults to False. Only applicable when using the default processors, as custom processors can be used to handle masking on their own.

  • handlers (Optional[List[Handlers]], optional) – List of log handlers (CONSOLE, FILE). Defaults to CONSOLE.

  • file_name (Optional[str], optional) – Filename for FILE handler. Required if FILE handler is used. Defaults to None.

  • **kwargs – Additional keyword arguments to be passed to the custom_processors_factory, if provided. disable_masking is automatically passed to the custom_processors_factory and should not be provided here. if custom_processors_factory does not accept any keyword arguments, they will all be ignored. Additionally, all kwargs receivable by the factory function must be hashable else TypeError will be raised by the _handle_and_patch_processor_factory_kwargs wrapper.

Returns:

A logger instance configured with the specified settings.

Return type:

PeakLogger

Raises:

ValueError – If the file_name is not provided for FILE handler or if multiple renderers are found in the processor`(s) list returned by the `custom_processors_factory.

peak.tools.logging.peak_contexts_processor(_, __, event_dict)

Add the standard attribute to the event_dict.

Parameters:
  • _ (str) –

  • __ (str) –

  • event_dict (MutableMapping[str, Any]) –

Return type:

MutableMapping[str, Any]

peak.tools.logging.pii_masking_processor(_, __, event_dict)

Masks sensitive PII data present in event_dict.

Parameters:
  • _ (str) –

  • __ (str) –

  • event_dict (MutableMapping[str, Any]) –

Return type:

MutableMapping[str, Any]