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.pod_id = pod_id
|
||||
|
||||
self.metadata = {
|
||||
'model_name': '-',
|
||||
'model_id': '-',
|
||||
'workflow_name': 'opc_repository',
|
||||
'schedule_name': '-'
|
||||
}
|
||||
|
||||
def set_security(self):
|
||||
"""
|
||||
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.client.application_uri = self.server_uri
|
||||
self.logger.info('Setting security...')
|
||||
self.logger.custom_info('Setting security...', self.metadata)
|
||||
self.client.set_security(
|
||||
SecurityPolicyBasic256,
|
||||
certificate=str(cert),
|
||||
@@ -107,7 +114,8 @@ class OpcRepository():
|
||||
self.client = Client(self.url)
|
||||
if self.cert_path:
|
||||
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()
|
||||
|
||||
def try_connect(self) -> tuple[bool, dict[str, Any]]:
|
||||
@@ -124,7 +132,7 @@ class OpcRepository():
|
||||
return True, {}
|
||||
except Exception as e:
|
||||
trace = traceback.format_exc()
|
||||
self.logger.error(trace)
|
||||
self.logger.custom_error(trace, self.metadata)
|
||||
|
||||
return False, {
|
||||
"notification_id": f"OPC_CONNECTION_ERROR_{self.id}",
|
||||
@@ -142,9 +150,11 @@ class OpcRepository():
|
||||
return
|
||||
try:
|
||||
self.client.disconnect()
|
||||
self.logger.info('Disconnected from OPC server')
|
||||
self.logger.custom_info(
|
||||
'Disconnected from OPC server', self.metadata)
|
||||
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
|
||||
|
||||
def __del__(self):
|
||||
@@ -154,7 +164,8 @@ class OpcRepository():
|
||||
try:
|
||||
self.disconnect()
|
||||
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]]:
|
||||
"""
|
||||
@@ -171,30 +182,31 @@ class OpcRepository():
|
||||
return self.connect()
|
||||
|
||||
if self.error_count > 5:
|
||||
self.logger.warning(
|
||||
f"OPC server {self.id} will be disconnected due to multiple errors")
|
||||
self.logger.custom_warning(
|
||||
f"OPC server {self.id} will be disconnected due to multiple errors", self.metadata)
|
||||
try:
|
||||
self.disconnect()
|
||||
except Exception as e:
|
||||
trace = traceback.format_exc()
|
||||
self.logger.error(f"Failed to disconnect from OPC server: {e}")
|
||||
self.logger.error(trace)
|
||||
self.logger.info(
|
||||
f"Attempting to reconnect to OPC server {self.id}...")
|
||||
self.logger.custom_error(
|
||||
f"Failed to disconnect from OPC server: {e}", self.metadata)
|
||||
self.logger.custom_error(trace, self.metadata)
|
||||
self.logger.custom_info(
|
||||
f"Attempting to reconnect to OPC server {self.id}...", self.metadata)
|
||||
return self.connect()
|
||||
|
||||
if hasattr(self.client, 'aio_obj') and self.client.aio_obj.uaclient.protocol is None or \
|
||||
(hasattr(self.client.aio_obj.uaclient, 'protocol') and
|
||||
self.client.aio_obj.uaclient.protocol.state == "closed"):
|
||||
|
||||
self.logger.error(
|
||||
f"OPC server {self.id} is not connected")
|
||||
self.logger.custom_error(
|
||||
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(
|
||||
) > self.reconnection_interval:
|
||||
self.disconnect()
|
||||
self.logger.error(
|
||||
f"Trying to reconnect to OPC server {self.id}...")
|
||||
self.logger.custom_info(
|
||||
f"Trying to reconnect to OPC server {self.id}...", self.metadata)
|
||||
return self.connect()
|
||||
|
||||
return False, {
|
||||
|
||||
Reference in New Issue
Block a user