From 33e451259e372c48846f66732a4d806e01229154 Mon Sep 17 00:00:00 2001 From: hhuseyinpay Date: Thu, 30 May 2024 22:38:17 +0300 Subject: [PATCH 1/4] feat: add optional param for passing a custom logger to ChargePoint --- ocpp/charge_point.py | 22 +++++++++++++--------- tests/test_charge_point.py | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/ocpp/charge_point.py b/ocpp/charge_point.py index 7046cada5..250a562bd 100644 --- a/ocpp/charge_point.py +++ b/ocpp/charge_point.py @@ -195,7 +195,7 @@ class ChargePoint: initiated and received by the Central System """ - def __init__(self, id, connection, response_timeout=30): + def __init__(self, id, connection, response_timeout=30, logger=LOGGER): """ Args: @@ -204,7 +204,8 @@ def __init__(self, id, connection, response_timeout=30): connection: Connection to CP. response_timeout (int): When no response on a request is received within this interval, a asyncio.TimeoutError is raised. - + logger (logging.Logger): The logger used to log messages. By default, it logs to the 'ocpp' logger. + This can be customized by passing a different logger instance. """ self.id = id @@ -232,10 +233,13 @@ def __init__(self, id, connection, response_timeout=30): # for testing purposes to have predictable unique ids. self._unique_id_generator = uuid.uuid4 + # The logger used to log messages. By default it logs to the 'ocpp' + self.logger = logger + async def start(self): while True: message = await self._connection.recv() - LOGGER.info("%s: receive message %s", self.id, message) + self.logger.info("%s: receive message %s", self.id, message) await self.route_message(message) @@ -250,7 +254,7 @@ async def route_message(self, raw_msg): try: msg = unpack(raw_msg) except OCPPError as e: - LOGGER.exception( + self.logger.exception( "Unable to parse message: '%s', it doesn't seem " "to be valid OCPP: %s", raw_msg, @@ -262,7 +266,7 @@ async def route_message(self, raw_msg): try: await self._handle_call(msg) except OCPPError as error: - LOGGER.exception("Error while handling request '%s'", msg) + self.logger.exception("Error while handling request '%s'", msg) response = msg.create_call_error(error).to_json() await self._send(response) @@ -313,7 +317,7 @@ async def _handle_call(self, msg): if inspect.isawaitable(response): response = await response except Exception as e: - LOGGER.exception("Error while handling request '%s'", msg) + self.logger.exception("Error while handling request '%s'", msg) response = msg.create_call_error(e).to_json() await self._send(response) @@ -415,7 +419,7 @@ async def call(self, payload, suppress=True, unique_id=None): ) if response.message_type_id == MessageType.CallError: - LOGGER.warning("Received a CALLError: %s'", response) + self.logger.warning("Received a CALLError: %s'", response) if suppress: return raise response.to_exception() @@ -446,7 +450,7 @@ async def _get_specific_response(self, unique_id, timeout): if response.unique_id == unique_id: return response - LOGGER.error("Ignoring response with unknown unique id: %s", response) + self.logger.error("Ignoring response with unknown unique id: %s", response) timeout_left = wait_until - time.time() if timeout_left < 0: @@ -455,5 +459,5 @@ async def _get_specific_response(self, unique_id, timeout): return await self._get_specific_response(unique_id, timeout_left) async def _send(self, message): - LOGGER.info("%s: send %s", self.id, message) + self.logger.info("%s: send %s", self.id, message) await self._connection.send(message) diff --git a/tests/test_charge_point.py b/tests/test_charge_point.py index b1ab1412b..1abc8535c 100644 --- a/tests/test_charge_point.py +++ b/tests/test_charge_point.py @@ -1,3 +1,4 @@ +import logging from dataclasses import asdict import pytest @@ -470,3 +471,17 @@ def after_boot_notification(self, *args, **kwargs): assert ChargerA.after_boot_notification_call_count == 1 assert ChargerB.on_boot_notification_call_count == 1 assert ChargerB.after_boot_notification_call_count == 1 + + +def test_custom_logger(): + class ChargePoint(cp_201): + pass + + # Create a custom logger + custom_logger = logging.getLogger('custom_logger') + + # Create a ChargePoint instance with the custom logger + charge_point = ChargePoint(id='123', connection=None, logger=custom_logger) + + # Check if the logger of the instance is the custom logger + assert charge_point.logger is custom_logger From 3fe614bc7c77dd12ae2ae6db2ba28bd945747c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20H=C3=BCseyin=20Pay?= Date: Tue, 24 Sep 2024 10:50:51 +0300 Subject: [PATCH 2/4] Update test_charge_point.py --- tests/test_charge_point.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_charge_point.py b/tests/test_charge_point.py index 5a9914fa5..ab7be8219 100644 --- a/tests/test_charge_point.py +++ b/tests/test_charge_point.py @@ -480,10 +480,10 @@ class ChargePoint(cp_201): pass # Create a custom logger - custom_logger = logging.getLogger('custom_logger') + custom_logger = logging.getLogger("custom_logger") # Create a ChargePoint instance with the custom logger - charge_point = ChargePoint(id='123', connection=None, logger=custom_logger) + charge_point = ChargePoint(id="123", connection=None, logger=custom_logger) # Check if the logger of the instance is the custom logger assert charge_point.logger is custom_logger From e67a4faba51d613e920bf8da4276f9c3d744b986 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20H=C3=BCseyin=20Pay?= Date: Tue, 24 Sep 2024 11:28:38 +0300 Subject: [PATCH 3/4] Update line width charge_point.py --- ocpp/charge_point.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ocpp/charge_point.py b/ocpp/charge_point.py index cba622a5b..268ce6865 100644 --- a/ocpp/charge_point.py +++ b/ocpp/charge_point.py @@ -206,8 +206,9 @@ def __init__(self, id, connection, response_timeout=30, logger=LOGGER): connection: Connection to CP. response_timeout (int): When no response on a request is received within this interval, a asyncio.TimeoutError is raised. - logger (logging.Logger): The logger used to log messages. By default, it logs to the 'ocpp' logger. - This can be customized by passing a different logger instance. + logger (logging.Logger): The logger used to log messages. By default, + it logs to the 'ocpp' logger. This can be customized by passing + a different logger instance. """ self.id = id From 5d9b5d93879125438950aa800cf17f3cb5a566bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20H=C3=BCseyin=20Pay?= Date: Wed, 25 Sep 2024 16:48:43 +0300 Subject: [PATCH 4/4] make code quality happy --- ocpp/charge_point.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ocpp/charge_point.py b/ocpp/charge_point.py index 268ce6865..d6075f8c3 100644 --- a/ocpp/charge_point.py +++ b/ocpp/charge_point.py @@ -206,9 +206,9 @@ def __init__(self, id, connection, response_timeout=30, logger=LOGGER): connection: Connection to CP. response_timeout (int): When no response on a request is received within this interval, a asyncio.TimeoutError is raised. - logger (logging.Logger): The logger used to log messages. By default, - it logs to the 'ocpp' logger. This can be customized by passing - a different logger instance. + logger (logging.Logger): The logger used to log messages. By default, it + logs to the 'ocpp' logger. This can be customized by passing a different + logger instance. """ self.id = id @@ -367,7 +367,7 @@ async def _handle_call(self, msg): return response async def call( - self, payload, suppress=True, unique_id=None, skip_schema_validation=False + self, payload, suppress=True, unique_id=None, skip_schema_validation=False ): """ Send Call message to client and return payload of response.