Add timestamps to the log output.
This commit is contained in:
@@ -3,10 +3,10 @@ from rich.markdown import Markdown
|
||||
|
||||
class MessagePrinter:
|
||||
@staticmethod
|
||||
def connected(ip, port):
|
||||
def connected(ip, port, timestamp):
|
||||
console = Console()
|
||||
|
||||
console.print(f'[yellow b][INFO][/] Connected to the server ({ip}:{port}).')
|
||||
console.print(f'[yellow b][INFO][/][#8abeb7 b][{timestamp}][/] Connected to the server ({ip}:{port}).')
|
||||
|
||||
@staticmethod
|
||||
def print(title: str, message: str):
|
||||
@@ -16,6 +16,11 @@ class MessagePrinter:
|
||||
|
||||
console.print(Markdown(full_message))
|
||||
|
||||
@staticmethod
|
||||
def printInfo(message: str, timestamp: str):
|
||||
console = Console()
|
||||
|
||||
console.print(f'[yellow b][INFO][/][#8abeb7 b][{timestamp}][/] {message}')
|
||||
|
||||
@staticmethod
|
||||
def bye():
|
||||
|
||||
@@ -23,6 +23,7 @@ import sys
|
||||
|
||||
from notification_client.MessagePrinter import MessagePrinter
|
||||
from notification_client.notitication import Notification
|
||||
from notification_client.timestamp import Timestamp
|
||||
|
||||
|
||||
class Client:
|
||||
@@ -35,9 +36,10 @@ class Client:
|
||||
async def connect(self):
|
||||
try:
|
||||
reader, writer = await asyncio.open_connection(self.__ip, self.__port)
|
||||
MessagePrinter.connected(self.__ip, self.__port)
|
||||
MessagePrinter.connected(self.__ip, self.__port, Timestamp.now())
|
||||
except ConnectionRefusedError:
|
||||
sys.stderr.write("Could not connect to the server. Please, check the ip and the port.\n")
|
||||
now = Timestamp.now()
|
||||
sys.stderr.write(f"[ERROR][{now}] Could not connect to the server. Please, check the ip and the port.\n")
|
||||
sys.exit(1)
|
||||
|
||||
await self.handle(reader, writer)
|
||||
@@ -54,6 +56,8 @@ class Client:
|
||||
title = await self.receive_message(reader)
|
||||
message = await self.receive_message(reader)
|
||||
|
||||
now = Timestamp.now()
|
||||
MessagePrinter.printInfo('A notification received from the server.', now)
|
||||
MessagePrinter.print(title, message)
|
||||
|
||||
Notification.notify(title, message)
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
from datetime import datetime
|
||||
|
||||
class Timestamp:
|
||||
@staticmethod
|
||||
def now() -> str:
|
||||
return str(datetime.now())
|
||||
|
||||
Reference in New Issue
Block a user