From c34a37e43f152f60c948c8eb67b12d4263c854c5 Mon Sep 17 00:00:00 2001 From: Michael Wellman Date: Wed, 28 Aug 2024 06:44:03 -0500 Subject: [PATCH] #1895 Provider Service log level (#1961) --- app/provider_details/provider_service.py | 40 ++++++++++++++++-------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/app/provider_details/provider_service.py b/app/provider_details/provider_service.py index a391bff567..07c1c9838f 100644 --- a/app/provider_details/provider_service.py +++ b/app/provider_details/provider_service.py @@ -1,4 +1,5 @@ -import logging +from flask import current_app + from app.dao.provider_details_dao import get_provider_details_by_id from app.exceptions import InvalidProviderException from app.models import Notification, ProviderDetails @@ -9,10 +10,6 @@ ) from typing import Type, Dict, Optional -logging.basicConfig(format='%(levelname)s %(asctime)s %(pathname)s:%(lineno)d: %(message)s') -logger = logging.getLogger('notification-api.provider_switching') -logger.setLevel(logging.DEBUG) - class ProviderService: def __init__(self): @@ -56,15 +53,18 @@ def get_provider( # This is a UUID (ProviderDetails primary key). provider_id = self._get_template_or_service_provider_id(notification) - logger.debug('notification = %s', notification) - logger.debug('provider_id = %s', provider_id) + current_app.logger.debug( + 'Provider service getting provider for notification = %s, provider_id = %s', notification.id, provider_id + ) if provider_id: provider = get_provider_details_by_id(provider_id) elif notification.notification_type != NotificationType.SMS: # Use an alternative strategy to determine the provider. provider_selection_strategy = self._strategies.get(NotificationType(notification.notification_type)) - logger.debug('Provider selection strategy: %s', provider_selection_strategy) + current_app.logger.debug( + 'Provider selection strategy: %s, for notification: %s', provider_selection_strategy, notification.id + ) provider = ( None if (provider_selection_strategy is None) @@ -89,7 +89,11 @@ def get_provider( elif not provider.active: raise InvalidProviderException(f'The provider {provider.display_name} is not active.') - logger.debug('Returning provider: %s', None if provider is None else provider.display_name) + current_app.logger.debug( + 'Returning provider: %s, for notification %s', + None if provider is None else provider.display_name, + notification.id, + ) return provider @staticmethod @@ -106,18 +110,28 @@ def _get_template_or_service_provider_id(notification: Notification) -> Optional # TODO #957 - The field is nullable, but what does SQLAlchemy return? An empty string? # Testing for None broke a user flows test; user flows is since removed but this is possibly an issue? if notification.template.provider_id: - logger.debug('Found template provider ID %s', notification.template.provider_id) + current_app.logger.debug( + 'Found template provider ID %s, for notification %s', notification.template.provider_id, notification.id + ) return notification.template.provider_id # A template provider_id is not available. Try using a service provider_id, which might also be None. if notification.notification_type == NotificationType.EMAIL.value: - logger.debug('Service provider e-mail ID %s', notification.service.email_provider_id) + current_app.logger.debug( + 'Service provider e-mail ID %s, for notification %s', + notification.service.email_provider_id, + notification.id, + ) return notification.service.email_provider_id elif notification.notification_type == NotificationType.SMS.value: - logger.debug('Service provider SMS ID %s', notification.service.sms_provider_id) + current_app.logger.debug( + 'Service provider SMS ID %s, for notification %s', notification.service.sms_provider_id, notification.id + ) return notification.service.sms_provider_id # TODO #957 - What about letters? That is the 3rd enumerated value in NotificationType # and Notification.notification_type. - logger.critical('Unanticipated notification type: %s', notification.notification_type) + current_app.logger.critical( + 'Unanticipated notification type: %s for notification %s', notification.notification_type, notification.id + ) return None