pynenc.util.log

Module Contents

Classes

Colors

ANSI color codes for terminal coloring.

ColoredFormatter

Custom formatter that adds colors to log output using ANSI color codes. No external dependencies required.

TaskLoggerAdapter

Logger adapter for tasks.

RunnerLogAdapter

Logger adapter for runners.

Functions

create_logger

Creates a logger for the specified app with timestamps and optional colored output.

API

class pynenc.util.log.Colors[source]

ANSI color codes for terminal coloring.

RESET

‘\x1b[0m’

RED

‘\x1b[31m’

GREEN

‘\x1b[32m’

YELLOW

‘\x1b[33m’

BLUE

‘\x1b[34m’

CYAN

‘\x1b[36m’

RED_BG

‘\x1b[41m’

WHITE

‘\x1b[37m’

class pynenc.util.log.ColoredFormatter(fmt: str | None = None, datefmt: str | None = None, style: typing.Literal[%, {, $] = '%', validate: bool = True)[source]

Bases: logging.Formatter

Custom formatter that adds colors to log output using ANSI color codes. No external dependencies required.

Initialization

Initialize the formatter with specified format strings.

Args: fmt: The format string for the message datefmt: The format string for datetime objects style: The style of the fmt string validate: Whether to validate the format string

LEVEL_COLORS

None

format(record: logging.LogRecord) str[source]
pynenc.util.log.create_logger(app: pynenc.app.Pynenc, use_colors: bool = True) logging.Logger[source]

Creates a logger for the specified app with timestamps and optional colored output.

Parameters:
  • app (Pynenc) – The app instance for which the logger is created.

  • use_colors (bool) – Whether to use colored output (defaults to True).

Returns:

The created logger.

Raises:

ValueError – If the logging level is invalid.

class pynenc.util.log.TaskLoggerAdapter(logger: logging.Logger, task_id: str, invocation_id: Optional[str] = None)[source]

Bases: logging.LoggerAdapter

Logger adapter for tasks.

This adapter adds task and invocation context to log messages.

Initialization

Initialize the adapter with a logger and a dict-like object which provides contextual information. This constructor signature allows easy stacking of LoggerAdapters, if so desired.

You can effectively pass keyword arguments as shown in the following example:

adapter = LoggerAdapter(someLogger, dict(p1=v1, p2=“v2”))

set_context(task_id: str, invocation_id: Optional[str]) None[source]

Sets the context for logging.

Parameters:
  • task_id (str) – The ID of the task.

  • invocation_id (Optional[str]) – The ID of the invocation.

process(msg: Any, kwargs: collections.abc.MutableMapping[str, Any]) tuple[source]

Processes a log message, adding task and invocation context.

Parameters:
  • msg (Any) – The log message.

  • kwargs (MutableMapping[str, Any]) – Additional keyword arguments.

Returns:

The processed message and kwargs.

class pynenc.util.log.RunnerLogAdapter(logger: logging.Logger, runner_id: str)[source]

Bases: logging.LoggerAdapter

Logger adapter for runners.

This adapter adds runner context to log messages.

Parameters:
  • logger (logging.Logger) – The logger instance.

  • runner_id (str) – The ID of the runner.

Initialization

Initialize the adapter with a logger and a dict-like object which provides contextual information. This constructor signature allows easy stacking of LoggerAdapters, if so desired.

You can effectively pass keyword arguments as shown in the following example:

adapter = LoggerAdapter(someLogger, dict(p1=v1, p2=“v2”))

process(msg: Any, kwargs: collections.abc.MutableMapping[str, Any]) tuple[source]

Processes a log message, adding runner context.

Parameters:
  • msg (Any) – The log message.

  • kwargs (MutableMapping[str, Any]) – Additional keyword arguments.

Returns:

The processed message and kwargs.