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

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