OpenFactory Logger API#
OpenFactory provides two classes for contextual logging:
openfactory.logging.loggers.OpenFactoryLoggeris the logger available throughself.loggerin every OpenFactory Application.openfactory.logging.loggers.OpenFactoryLoggerAdapteris returned byOpenFactoryLogger.with_contextand represents a logger with additional persistent context.
OpenFactoryLogger#
- class openfactory.logging.loggers.OpenFactoryLogger(name, level=0)[source]#
Bases:
LoggerOpenFactory 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_uuidonce instead of passing it to every logging call.- Return type:
- 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:
LoggerAdapterLogger 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
extravalues supplied for the individual logging call. If both define the same key, the adapter context takes precedence.
- with_context(**kwargs)[source]#
Create a new logger with additional context.
The existing context is preserved.
- Return type:
- 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")