"""Logging utils.""" import uuid from owslogger import logger # FIXME: Placeholder wrapper for expected extra fields. class MetadataIngesterAdapter(logger.OwsLoggingAdapter): """Custom log adapter to log message id and message thread id.""" def process(self, msg, kwargs): """Process log message to add contextual information. This takes out the message thread id and message id and prepends it to the log message. Args: msg (obj): the log message. """ super().process(msg, kwargs) return msg, kwargs def get_logger(app_logger, correlation_id=None): """Get logger with a correlation_id attached. Args: app_logger: ows-logger instance correlation_id: correlation_id that will be sent along with log record Returns: MetadataIngesterAdapter: instance of DdexIngesterAdapter with correlation_id and other message ids attached. """ return MetadataIngesterAdapter( app_logger, { 'correlation_id': correlation_id or uuid.uuid4() } ) def update_logger_correlation_id(logger, correlation_id): """Set logger correlation_id.""" logger.extra = { **logger.extra, 'correlation_id': correlation_id }