From 2cbbed5e9231a78d79b333a267cb9c283add3fc6 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 30 Jun 2022 10:48:59 -0400 Subject: [PATCH] chore: Edit logger.error calls (#1019) * Edit logger.errors that don't need to be called as errors to warning or debug --- .../iot/device/common/handle_exceptions.py | 4 +- .../azure/iot/device/common/mqtt_transport.py | 38 ++++++++++--------- .../common/pipeline/pipeline_stages_base.py | 10 ++--- .../device/iothub/pipeline/mqtt_pipeline.py | 14 ++++--- .../pipeline/pipeline_stages_iothub_mqtt.py | 2 +- .../provisioning/pipeline/mqtt_pipeline.py | 2 +- 6 files changed, 37 insertions(+), 33 deletions(-) diff --git a/azure-iot-device/azure/iot/device/common/handle_exceptions.py b/azure-iot-device/azure/iot/device/common/handle_exceptions.py index b2f6fc1a7..fdb5e89bc 100644 --- a/azure-iot-device/azure/iot/device/common/handle_exceptions.py +++ b/azure-iot-device/azure/iot/device/common/handle_exceptions.py @@ -25,8 +25,8 @@ def handle_background_exception(e): # @FUTURE: We should add a mechanism which allows applications to receive these # exceptions so they can respond accordingly - logger.error(msg="Exception caught in background thread. Unable to handle.") - logger.error(traceback.format_exception_only(type(e), e)) + logger.warning(msg="Exception caught in background thread. Unable to handle.") + logger.warning(traceback.format_exception_only(type(e), e)) def swallow_unraised_exception(e, log_msg=None, log_lvl="warning"): diff --git a/azure-iot-device/azure/iot/device/common/mqtt_transport.py b/azure-iot-device/azure/iot/device/common/mqtt_transport.py index 0001a69e8..cf54a0416 100644 --- a/azure-iot-device/azure/iot/device/common/mqtt_transport.py +++ b/azure-iot-device/azure/iot/device/common/mqtt_transport.py @@ -182,20 +182,22 @@ def on_connect(client, userdata, flags, rc): _create_error_from_connack_rc_code(rc) ) except Exception: - logger.error("Unexpected error calling on_mqtt_connection_failure_handler") - logger.error(traceback.format_exc()) + logger.warning( + "Unexpected error calling on_mqtt_connection_failure_handler" + ) + logger.warning(traceback.format_exc()) else: - logger.error( + logger.warning( "connection failed, but no on_mqtt_connection_failure_handler handler callback provided" ) elif this.on_mqtt_connected_handler: try: this.on_mqtt_connected_handler() except Exception: - logger.error("Unexpected error calling on_mqtt_connected_handler") - logger.error(traceback.format_exc()) + logger.warning("Unexpected error calling on_mqtt_connected_handler") + logger.warning(traceback.format_exc()) else: - logger.error("No event handler callback set for on_mqtt_connected_handler") + logger.debug("No event handler callback set for on_mqtt_connected_handler") def on_disconnect(client, userdata, rc): this = self_weakref() @@ -220,10 +222,10 @@ def on_disconnect(client, userdata, rc): try: this.on_mqtt_disconnected_handler(cause) except Exception: - logger.error("Unexpected error calling on_mqtt_disconnected_handler") - logger.error(traceback.format_exc()) + logger.warning("Unexpected error calling on_mqtt_disconnected_handler") + logger.warning(traceback.format_exc()) else: - logger.error("No event handler callback set for on_mqtt_disconnected_handler") + logger.warning("No event handler callback set for on_mqtt_disconnected_handler") def on_subscribe(client, userdata, mid, granted_qos): this = self_weakref() @@ -254,10 +256,10 @@ def on_message(client, userdata, mqtt_message): try: this.on_mqtt_message_received_handler(mqtt_message.topic, mqtt_message.payload) except Exception: - logger.error("Unexpected error calling on_mqtt_message_received_handler") - logger.error(traceback.format_exc()) + logger.warning("Unexpected error calling on_mqtt_message_received_handler") + logger.warning(traceback.format_exc()) else: - logger.error( + logger.debug( "No event handler callback set for on_mqtt_message_received_handler - DROPPING MESSAGE" ) @@ -601,8 +603,8 @@ def establish_operation(self, mid, callback=None): try: callback() except Exception: - logger.error("Unexpected error calling callback for MID: {}".format(mid)) - logger.error(traceback.format_exc()) + logger.debug("Unexpected error calling callback for MID: {}".format(mid)) + logger.debug(traceback.format_exc()) else: # Not entirely unexpected because of QOS=1 logger.debug("No callback for MID: {}".format(mid)) @@ -644,8 +646,8 @@ def complete_operation(self, mid): try: callback() except Exception: - logger.error("Unexpected error calling callback for MID: {}".format(mid)) - logger.error(traceback.format_exc()) + logger.debug("Unexpected error calling callback for MID: {}".format(mid)) + logger.debug(traceback.format_exc()) else: # fully expected. QOS=1 means we might get 2 PUBACKs logger.debug("No callback set for MID: {}".format(mid)) @@ -674,7 +676,7 @@ def cancel_all_operations(self): try: callback(cancelled=True) except Exception: - logger.error("Unexpected error calling callback for MID: {}".format(mid)) - logger.error(traceback.format_exc()) + logger.debug("Unexpected error calling callback for MID: {}".format(mid)) + logger.debug(traceback.format_exc()) else: logger.debug("Cancelling {} - No callback set for MID".format(mid)) diff --git a/azure-iot-device/azure/iot/device/common/pipeline/pipeline_stages_base.py b/azure-iot-device/azure/iot/device/common/pipeline/pipeline_stages_base.py index e251920e5..c6fa7fb88 100644 --- a/azure-iot-device/azure/iot/device/common/pipeline/pipeline_stages_base.py +++ b/azure-iot-device/azure/iot/device/common/pipeline/pipeline_stages_base.py @@ -149,16 +149,16 @@ def handle_pipeline_event(self, event): except Exception as e: # Do not use exc_info parameter on logger.* calls. This causes pytest to save the # traceback which saves stack frames which shows up as a leak - logger.error( + logger.warning( msg="{}: Unexpected error in ._handle_pipeline_event() call: {}".format(self, e) ) if self.previous: - logger.error("{}: Raising background exception") + logger.warning("{}: Raising background exception") self.report_background_exception(e) else: # Nothing else we can do but log this. There exists no stage we can send the # exception to, and raising would send the error back down the pipeline. - logger.error( + logger.warning( "{}: Cannot report a background exception because there is no previous stage!" ) @@ -187,7 +187,7 @@ def send_op_down(self, op): self.next.run_op(op) else: # This shouldn't happen if the pipeline was created correctly - logger.error( + logger.warning( "{}({}): no next stage.cannot send op down. completing with error".format( self.name, op.name ) @@ -344,7 +344,7 @@ def _handle_pipeline_event(self, event): ) else: # unexpected condition: we should be handling all pipeline events - logger.error("incoming {} event with no handler. dropping.".format(event.name)) + logger.debug("incoming {} event with no handler. dropping.".format(event.name)) # NOTE: This stage could be a candidate for being refactored into some kind of other diff --git a/azure-iot-device/azure/iot/device/iothub/pipeline/mqtt_pipeline.py b/azure-iot-device/azure/iot/device/iothub/pipeline/mqtt_pipeline.py index 07a5bb5a7..fdea45b99 100644 --- a/azure-iot-device/azure/iot/device/iothub/pipeline/mqtt_pipeline.py +++ b/azure-iot-device/azure/iot/device/iothub/pipeline/mqtt_pipeline.py @@ -121,28 +121,28 @@ def _on_pipeline_event(event): if self.on_c2d_message_received: self.on_c2d_message_received(event.message) else: - logger.error("C2D message event received with no handler. dropping.") + logger.debug("C2D message event received with no handler. dropping.") elif isinstance(event, pipeline_events_iothub.InputMessageEvent): if self.on_input_message_received: self.on_input_message_received(event.message) else: - logger.error("input message event received with no handler. dropping.") + logger.debug("input message event received with no handler. dropping.") elif isinstance(event, pipeline_events_iothub.MethodRequestEvent): if self.on_method_request_received: self.on_method_request_received(event.method_request) else: - logger.error("Method request event received with no handler. Dropping.") + logger.debug("Method request event received with no handler. Dropping.") elif isinstance(event, pipeline_events_iothub.TwinDesiredPropertiesPatchEvent): if self.on_twin_patch_received: self.on_twin_patch_received(event.patch) else: - logger.error("Twin patch event received with no handler. Dropping.") + logger.debug("Twin patch event received with no handler. Dropping.") else: - logger.error("Dropping unknown pipeline event {}".format(event.name)) + logger.debug("Dropping unknown pipeline event {}".format(event.name)) def _on_connected(): if self.on_connected: @@ -515,7 +515,9 @@ def enable_feature(self, feature_name, callback): def on_complete(op, error): if error: - logger.error("Subscribe for {} failed. Not enabling feature".format(feature_name)) + logger.warning( + "Subscribe for {} failed. Not enabling feature".format(feature_name) + ) else: self.feature_enabled[feature_name] = True callback(error=error) diff --git a/azure-iot-device/azure/iot/device/iothub/pipeline/pipeline_stages_iothub_mqtt.py b/azure-iot-device/azure/iot/device/iothub/pipeline/pipeline_stages_iothub_mqtt.py index 57b4a44e7..128d2c04d 100644 --- a/azure-iot-device/azure/iot/device/iothub/pipeline/pipeline_stages_iothub_mqtt.py +++ b/azure-iot-device/azure/iot/device/iothub/pipeline/pipeline_stages_iothub_mqtt.py @@ -168,7 +168,7 @@ def _get_feature_subscription_topic(self, feature): elif feature == pipeline_constant.TWIN_PATCHES: return mqtt_topic_iothub.get_twin_patch_topic_for_subscribe() else: - logger.error("Cannot retrieve MQTT topic for subscription to invalid feature") + logger.warning("Cannot retrieve MQTT topic for subscription to invalid feature") raise pipeline_exceptions.OperationError( "Trying to enable/disable invalid feature - {}".format(feature) ) diff --git a/azure-iot-device/azure/iot/device/provisioning/pipeline/mqtt_pipeline.py b/azure-iot-device/azure/iot/device/provisioning/pipeline/mqtt_pipeline.py index 84895c517..afa07107b 100644 --- a/azure-iot-device/azure/iot/device/provisioning/pipeline/mqtt_pipeline.py +++ b/azure-iot-device/azure/iot/device/provisioning/pipeline/mqtt_pipeline.py @@ -104,7 +104,7 @@ def __init__(self, pipeline_configuration): def _on_pipeline_event(event): # error because no events should - logger.error("Dropping unknown pipeline event {}".format(event.name)) + logger.debug("Dropping unknown pipeline event {}".format(event.name)) def _on_connected(): if self.on_connected: