OpenFactory Logger API#

OpenFactory provides two classes for contextual logging:

OpenFactoryLogger#

class openfactory.logging.loggers.OpenFactoryLogger(name, level=0)[source]#

Bases: Logger

OpenFactory logger with support for contextual logging.

with_context(**kwargs)[source]#

Create a logger that automatically adds context to every log entry.

This is useful when a component repeatedly logs information about the same object. For example, a device monitor can attach its asset_uuid once instead of passing it to every logging call.

Return type:

OpenFactoryLoggerAdapter

Parameters:

**kwargs – Context fields to include in every log entry.

Returns:

OpenFactoryLoggerAdapter – Logger with the additional context.

Usage Example

logger = self.logger.with_context(
    asset_uuid=device.uuid,
)

logger.info("Connected")

OpenFactoryLoggerAdapter#

class openfactory.logging.loggers.OpenFactoryLoggerAdapter(logger, extra=None)[source]#

Bases: LoggerAdapter

Logger that automatically adds contextual information to log entries.

Instances are created using OpenFactoryLogger.with_context() and may themselves be extended with additional context.

process(msg, kwargs)[source]#

Process a logging call.

Merges the adapter context with any extra values supplied for the individual logging call. If both define the same key, the adapter context takes precedence.

Return type:

tuple[object, dict[str, Any]]

Parameters:
  • msg (str) – Log message.

  • kwargs (dict) – Keyword arguments passed to the logging call.

Returns:

tuple[str, dict] – Processed message and keyword arguments.

with_context(**kwargs)[source]#

Create a new logger with additional context.

The existing context is preserved.

Return type:

OpenFactoryLoggerAdapter

Parameters:

**kwargs – Additional context fields.

Returns:

OpenFactoryLoggerAdapter – Logger with the combined context.

Usage Example

gateway_logger = self.logger.with_context(
    gateway="opcua",
)

device_logger = gateway_logger.with_context(
    asset_uuid=device.uuid,
)

device_logger.info("Connected")