From 152e4a852be85e1e6dce38ef7e624df599462003 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Thu, 14 Dec 2023 15:30:42 +0200 Subject: [PATCH] Add timestamps to the log output. --- notification_server/server.py | 21 ++++++++++++++------- notification_server/timestamp.py | 7 +++++++ 2 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 notification_server/timestamp.py diff --git a/notification_server/server.py b/notification_server/server.py index b8c5a89..85909ed 100644 --- a/notification_server/server.py +++ b/notification_server/server.py @@ -20,6 +20,7 @@ import struct from rich.console import Console import asyncio +from notification_server.timestamp import Timestamp class Server: @@ -46,8 +47,9 @@ class Server: self.__ip, self.__producer_port ) - self.console.print(f'[yellow b][INFO][/] Serves on {self.__ip}:{self.__port} for clients.') - self.console.print(f'[yellow b][INFO][/] Receives notifications from producers at {self.__ip}:{self.__producer_port}.') + now = Timestamp.now() + self.console.print(f'[yellow b][INFO][/][#8abeb7 b][{now}][/] Serves on {self.__ip}:{self.__port} for clients.') + self.console.print(f'[yellow b][INFO][/][#8abeb7 b][{now}][/] Receives notifications from producers at {self.__ip}:{self.__producer_port}.') async with self.__producer_socket, self.__client_socket: await asyncio.gather( @@ -72,7 +74,8 @@ class Server: async def handle_producer(self, reader, writer, notification_dir): addr = writer.get_extra_info('peername') - self.console.print(f"[yellow b][INFO][/] A producer ({addr[0]}:{addr[1]}) connected.") + now = Timestamp.now() + self.console.print(f"[yellow b][INFO][/][#8abeb7 b][{now}][/] A producer ({addr[0]}:{addr[1]}) connected.") try: while True: @@ -80,7 +83,8 @@ class Server: message = await self.receive_message(reader) if title == '' and message == '': - self.console.print(f"[yellow b][INFO][/] The producer ({addr[0]}:{addr[1]}) finished.") + now = Timestamp.now() + self.console.print(f"[yellow b][INFO][/][#8abeb7 b][{now}][/] The producer ({addr[0]}:{addr[1]}) finished.") break await self.broadcast_message(title) @@ -89,12 +93,14 @@ class Server: except asyncio.CancelledError: pass finally: - self.console.print(f"[yellow b][INFO][/] The producer ({addr[0]}:{addr[1]}) disconnected.") + now = Timestamp.now() + self.console.print(f"[yellow b][INFO][/][#8abeb7 b][{now}][/] The producer ({addr[0]}:{addr[1]}) disconnected.") writer.close() async def handle_client(self, reader, writer): addr = writer.get_extra_info('peername') - self.console.print(f"[yellow b][INFO][/] A client ({addr[0]}:{addr[1]}) connected.") + now = Timestamp.now() + self.console.print(f"[yellow b][INFO][/][#8abeb7 b][{now}][/] A client ({addr[0]}:{addr[1]}) connected.") self.__client_writers.append(writer) try: @@ -105,7 +111,8 @@ class Server: except asyncio.CancelledError: pass finally: - self.console.print(f"[yellow b][INFO][/] The client ({addr[0]}:{addr[1]}) disconnected.") + now = Timestamp.now() + self.console.print(f"[yellow b][INFO][/][#8abeb7 b][{now}][/] The client ({addr[0]}:{addr[1]}) disconnected.") self.__client_writers.remove(writer) writer.close() diff --git a/notification_server/timestamp.py b/notification_server/timestamp.py new file mode 100644 index 0000000..3571f02 --- /dev/null +++ b/notification_server/timestamp.py @@ -0,0 +1,7 @@ +from datetime import datetime + +class Timestamp: + @staticmethod + def now() -> str: + return str(datetime.now()) +