SIENTIAPDE-1199
Enhance logging in OpcRepository to include metadata for all log messages - Introduced a metadata dictionary to the OpcRepository class for improved context in logging. - Updated logging statements to utilize custom_info, custom_warning, and custom_error methods, ensuring all key operations and error messages include relevant metadata. - Improved visibility of connection status, disconnection attempts, and error handling, facilitating better monitoring and debugging.
This commit is contained in:
@@ -55,6 +55,13 @@ class OpcRepository():
|
|||||||
self.client = None
|
self.client = None
|
||||||
self.pod_id = pod_id
|
self.pod_id = pod_id
|
||||||
|
|
||||||
|
self.metadata = {
|
||||||
|
'model_name': '-',
|
||||||
|
'model_id': '-',
|
||||||
|
'workflow_name': 'opc_repository',
|
||||||
|
'schedule_name': '-'
|
||||||
|
}
|
||||||
|
|
||||||
def set_security(self):
|
def set_security(self):
|
||||||
"""
|
"""
|
||||||
Configures the security settings for the OPC UA client.
|
Configures the security settings for the OPC UA client.
|
||||||
@@ -84,7 +91,7 @@ class OpcRepository():
|
|||||||
self.server_cert_path) if self.server_cert_path else None
|
self.server_cert_path) if self.server_cert_path else None
|
||||||
|
|
||||||
self.client.application_uri = self.server_uri
|
self.client.application_uri = self.server_uri
|
||||||
self.logger.info('Setting security...')
|
self.logger.custom_info('Setting security...', self.metadata)
|
||||||
self.client.set_security(
|
self.client.set_security(
|
||||||
SecurityPolicyBasic256,
|
SecurityPolicyBasic256,
|
||||||
certificate=str(cert),
|
certificate=str(cert),
|
||||||
@@ -107,7 +114,8 @@ class OpcRepository():
|
|||||||
self.client = Client(self.url)
|
self.client = Client(self.url)
|
||||||
if self.cert_path:
|
if self.cert_path:
|
||||||
self.set_security()
|
self.set_security()
|
||||||
self.logger.info(f'Starting connection to OPC server {self.id}...')
|
self.logger.custom_info(
|
||||||
|
f'Starting connection to OPC server {self.id}...', self.metadata)
|
||||||
return self.try_connect()
|
return self.try_connect()
|
||||||
|
|
||||||
def try_connect(self) -> tuple[bool, dict[str, Any]]:
|
def try_connect(self) -> tuple[bool, dict[str, Any]]:
|
||||||
@@ -124,7 +132,7 @@ class OpcRepository():
|
|||||||
return True, {}
|
return True, {}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
trace = traceback.format_exc()
|
trace = traceback.format_exc()
|
||||||
self.logger.error(trace)
|
self.logger.custom_error(trace, self.metadata)
|
||||||
|
|
||||||
return False, {
|
return False, {
|
||||||
"notification_id": f"OPC_CONNECTION_ERROR_{self.id}",
|
"notification_id": f"OPC_CONNECTION_ERROR_{self.id}",
|
||||||
@@ -142,9 +150,11 @@ class OpcRepository():
|
|||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
self.client.disconnect()
|
self.client.disconnect()
|
||||||
self.logger.info('Disconnected from OPC server')
|
self.logger.custom_info(
|
||||||
|
'Disconnected from OPC server', self.metadata)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error(f"Failed to disconnect from OPC server: {e}")
|
self.logger.custom_error(
|
||||||
|
f"Failed to disconnect from OPC server: {e}", self.metadata)
|
||||||
self.client = None
|
self.client = None
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
@@ -154,7 +164,8 @@ class OpcRepository():
|
|||||||
try:
|
try:
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error(f"Error in destructor: {e}")
|
self.logger.custom_error(
|
||||||
|
f"Error in destructor: {e}", self.metadata)
|
||||||
|
|
||||||
def validate_connection(self) -> tuple[bool, dict[str, Any]]:
|
def validate_connection(self) -> tuple[bool, dict[str, Any]]:
|
||||||
"""
|
"""
|
||||||
@@ -171,30 +182,31 @@ class OpcRepository():
|
|||||||
return self.connect()
|
return self.connect()
|
||||||
|
|
||||||
if self.error_count > 5:
|
if self.error_count > 5:
|
||||||
self.logger.warning(
|
self.logger.custom_warning(
|
||||||
f"OPC server {self.id} will be disconnected due to multiple errors")
|
f"OPC server {self.id} will be disconnected due to multiple errors", self.metadata)
|
||||||
try:
|
try:
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
trace = traceback.format_exc()
|
trace = traceback.format_exc()
|
||||||
self.logger.error(f"Failed to disconnect from OPC server: {e}")
|
self.logger.custom_error(
|
||||||
self.logger.error(trace)
|
f"Failed to disconnect from OPC server: {e}", self.metadata)
|
||||||
self.logger.info(
|
self.logger.custom_error(trace, self.metadata)
|
||||||
f"Attempting to reconnect to OPC server {self.id}...")
|
self.logger.custom_info(
|
||||||
|
f"Attempting to reconnect to OPC server {self.id}...", self.metadata)
|
||||||
return self.connect()
|
return self.connect()
|
||||||
|
|
||||||
if hasattr(self.client, 'aio_obj') and self.client.aio_obj.uaclient.protocol is None or \
|
if hasattr(self.client, 'aio_obj') and self.client.aio_obj.uaclient.protocol is None or \
|
||||||
(hasattr(self.client.aio_obj.uaclient, 'protocol') and
|
(hasattr(self.client.aio_obj.uaclient, 'protocol') and
|
||||||
self.client.aio_obj.uaclient.protocol.state == "closed"):
|
self.client.aio_obj.uaclient.protocol.state == "closed"):
|
||||||
|
|
||||||
self.logger.error(
|
self.logger.custom_error(
|
||||||
f"OPC server {self.id} is not connected")
|
f"OPC server {self.id} is not connected", self.metadata)
|
||||||
|
|
||||||
if self.last_reconnection_time is None or (datetime.now() - self.last_reconnection_time).total_seconds(
|
if self.last_reconnection_time is None or (datetime.now() - self.last_reconnection_time).total_seconds(
|
||||||
) > self.reconnection_interval:
|
) > self.reconnection_interval:
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
self.logger.error(
|
self.logger.custom_info(
|
||||||
f"Trying to reconnect to OPC server {self.id}...")
|
f"Trying to reconnect to OPC server {self.id}...", self.metadata)
|
||||||
return self.connect()
|
return self.connect()
|
||||||
|
|
||||||
return False, {
|
return False, {
|
||||||
|
|||||||
Reference in New Issue
Block a user