def test_logs_correct_string(self, now): now.return_value.strftime.return_value = 'faketime' expected_message = str("INFO:project.services.logging_service:" "faketime\tcall_to_mixpanel\tchannel=subspace") with self.assertLogs(logging_service.logger, INFO) as logs: logging_service.format_and_log('call_to_mixpanel', channel='subspace') self.assertEqual(logs.output, [expected_message])
def test_logs_correct_string(self, now): now.return_value.strftime.return_value = 'faketime' expected_message = str( "INFO:project.services.logging_service:" "faketime\tcall_to_mixpanel\tchannel=subspace") with self.assertLogs(logging_service.logger, INFO) as logs: logging_service.format_and_log( 'call_to_mixpanel', channel='subspace') self.assertEqual(logs.output, [expected_message])
def log_to_mixpanel(distinct_id, event_name, **data): client = get_mixpanel_client() divert = getattr(settings, 'DIVERT_REMOTE_CONNECTIONS', False) mixpanel_kwargs = dict(distinct_id=distinct_id, event_name=event_name, properties=data) if client and not divert: client.track(**mixpanel_kwargs) logging_service.format_and_log(log_type='call_to_mixpanel', **mixpanel_kwargs)
def log_to_mixpanel(distinct_id, event_name, **data): client = get_mixpanel_client() divert = getattr(settings, 'DIVERT_REMOTE_CONNECTIONS', False) mixpanel_kwargs = dict( distinct_id=distinct_id, event_name=event_name, properties=data) if client and not divert: client.track(**mixpanel_kwargs) logging_service.format_and_log( log_type='call_to_mixpanel', **mixpanel_kwargs)
def mailgun_email_validator(value): message = _('The email address you entered does not appear to exist.') suggestion_template = ' Did you mean {}?' try: email_is_good, suggestion = validate_email_with_mailgun(value) if not email_is_good: if suggestion: message += suggestion_template.format(suggestion) raise ValidationError(message) except MailgunAPIError as err: send_email_to_admins(subject="Unexpected MailgunAPIError", message="{}".format(err)) format_and_log('mailgun_api_error', level='error', exception=str(err))
def mailgun_email_validator(value): message = _('The email address you entered does not appear to exist.') suggestion_template = ' Did you mean {}?' try: email_is_good, suggestion = validate_email_with_mailgun(value) if not email_is_good: if suggestion: message += suggestion_template.format(suggestion) raise ValidationError(message) except MailgunAPIError as err: send_email_to_admins( subject="Unexpected MailgunAPIError", message="{}".format(err)) format_and_log( 'mailgun_api_error', level='error', exception=str(err))
def check_for_session_based_redirects(self): session_based_redirect = super().check_for_session_based_redirects() if session_based_redirect: return session_based_redirect # get county_slugs earlier than dispatch self.county_slugs = self.session_data.getlist('counties', []) has_form_data = self.has_form_data_in_session() if not has_form_data: error_message = "{} with insufficient form data in session".format( self.__class__.__name__) format_and_log( 'application_error', level='error', error_message=error_message) if not has_form_data: return redirect(reverse('intake-county_application'))
def check_for_session_based_redirects(self): session_based_redirect = super().check_for_session_based_redirects() if session_based_redirect: return session_based_redirect # get county_slugs earlier than dispatch self.county_slugs = self.session_data.getlist('counties', []) has_form_data = self.has_form_data_in_session() if not has_form_data: error_message = "{} with insufficient form data in session".format( self.__class__.__name__) format_and_log('application_error', level='error', error_message=error_message) if not has_form_data: return redirect(reverse('intake-county_application'))
def form_validation_failed(view, errors): """ security concerns addressed here: MP gets just keys (avoid PII) std_out gets k-v pair for errors, but misses application identifiers """ event_name = 'application_errors' applicant = ApplicantsService.get_applicant_from_request_or_session( view.request) for error_key, errors in errors.items(): log_to_mixpanel.delay( distinct_id=applicant.get_uuid(), event_name=event_name, error=error_key, **mixpanel_applicant_data(applicant), **mixpanel_data_from_view_request_user(view)) LoggingService.format_and_log( event_name, url=view.request.path, view_name=view.__class__.__name__, field=error_key, errors=errors)
def form_validation_failed(view, request, errors): """ security concerns addressed here: MP gets just keys (avoid PII) std_out gets k-v pair for errors, but misses application identifiers """ event_name = 'application_errors' applicant = ApplicantsService.get_applicant_from_request_or_session( request) for error_key, errors in errors.items(): log_to_mixpanel.delay( distinct_id=applicant.get_uuid(), event_name=event_name, url=request.path, view_name=view.__class__.__name__, error=error_key) LoggingService.format_and_log( event_name, url=request.path, view_name=view.__class__.__name__, field=error_key, errors=errors)
def test_doesnt_log_with_invalid_level(self): with self.assertRaises(AttributeError): logging_service.format_and_log('set_phasers', level='stun')
def test_doesnt_log_without_valid_name(self): with self.assertRaises(TypeError): logging_service.format_and_log()