SIENTIAPDE-1110

Refactor logging in OPC and OpcRepository to utilize custom logging methods, enhancing error handling and traceability with metadata integration.
This commit is contained in:
vitor-aignosi
2025-06-27 12:24:15 -03:00
parent 119decac16
commit 566818b308
2 changed files with 8 additions and 10 deletions

View File

@@ -143,8 +143,8 @@ class OPC(BaseActivity):
if not success: if not success:
data['prediction_confidence'] = OPC_WRITTING_ERROR_CONFIDENCE data['prediction_confidence'] = OPC_WRITTING_ERROR_CONFIDENCE
self.debug( self.debug(
"Some data could not be written to OPC servers, setting confidence to " f"Some data could not be written to OPC servers, setting confidence to {OPC_WRITTING_ERROR_CONFIDENCE}.",
f"{OPC_WRITTING_ERROR_CONFIDENCE}." metadata
) )
else: else:

View File

@@ -1,5 +1,4 @@
import traceback import traceback
from logging import Logger
from datetime import datetime from datetime import datetime
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
@@ -9,8 +8,7 @@ from asyncua.ua import DataValue, Variant, VariantType
from regex import F from regex import F
from sientia_do.notifications.handlers import NotificationHandler from sientia_do.notifications.handlers import NotificationHandler
from sientia_do.notifications.models import NotificationLevel from sientia_do.notifications.models import NotificationLevel
from sientia_do.temporal.utils.logger import Logger
from laborious.activities.opc import OPC
data_type_map = { data_type_map = {
'float': { 'float': {
@@ -195,7 +193,7 @@ class OpcRepository():
return True return True
def write_data(self, node: str, value: Any, data_type: str, def write_data(self, node: str, value: Any, data_type: str,
logger: OPC, metadata: dict[str, Any]) -> bool: logger: Logger, metadata: dict[str, Any]) -> bool:
""" """
Writes data to the OPC server. Writes data to the OPC server.
If the connection is not established, it attempts to reconnect. If the connection is not established, it attempts to reconnect.
@@ -219,7 +217,7 @@ class OpcRepository():
level=NotificationLevel.ERROR, level=NotificationLevel.ERROR,
attachment_content=trace attachment_content=trace
) )
logger.error(trace, metadata) logger.custom_error(trace, metadata)
self.error_count += 1 self.error_count += 1
return False return False
@@ -230,11 +228,11 @@ class OpcRepository():
block="opc_repository", block="opc_repository",
level=NotificationLevel.ERROR level=NotificationLevel.ERROR
) )
logger.error(f"Unsupported data type: {data_type}", metadata)
return False return False
data = data_type_map[data_type]['converter'](value) data = data_type_map[data_type]['converter'](value)
logger.info(f'Writing {data} - {type(data)} to {node}', metadata) logger.custom_info(
f'Writing {data} - {type(data)} to {node}', metadata)
ua_data = DataValue( ua_data = DataValue(
Variant(data, data_type_map[data_type]['opc_type'])) Variant(data, data_type_map[data_type]['opc_type']))
@@ -249,7 +247,7 @@ class OpcRepository():
level=NotificationLevel.ERROR, level=NotificationLevel.ERROR,
attachment_content=trace attachment_content=trace
) )
logger.error(trace, metadata) logger.custom_error(trace, metadata)
self.error_count += 1 self.error_count += 1
return False return False
self.error_count = 0 self.error_count = 0