From daf8664fbe9e29c15a3c00ec85381bf0a24e1787 Mon Sep 17 00:00:00 2001 From: mrtedn21 Date: Tue, 21 Nov 2023 11:48:46 +0700 Subject: [PATCH] add logging --- .github/workflows/test.yml | 1 + martin_eden/core.py | 26 +++++++++++++++++++++----- martin_eden/logs.py | 15 +++++++++++++++ martin_eden/settings.py | 2 +- pyproject.toml | 2 +- 5 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 martin_eden/logs.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 26fbbee..a23e36b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,6 +6,7 @@ env: POSTGRES_URL: ${{ vars.POSTGRES_URL }} SERVER_HOST: ${{ vars.SERVER_HOST }} SERVER_PORT: ${{ vars.SERVER_PORT }} + LOG_LEVEL: ${{ vars.LOG_LEVEL }} jobs: test: diff --git a/martin_eden/core.py b/martin_eden/core.py index b3414b5..c4a6657 100644 --- a/martin_eden/core.py +++ b/martin_eden/core.py @@ -1,5 +1,7 @@ import asyncio +from logging import getLogger import dataclasses +from martin_eden.logs import configure_logging import json import socket from asyncio import AbstractEventLoop @@ -155,8 +157,14 @@ class Backend: def __init__(self) -> None: self.event_loop: Optional[AbstractEventLoop] = None self.server_socket: Optional[socket.socket] = None + + self.settings = Settings() + configure_logging(self.settings.log_level) + self.logger = getLogger() + self._configure_sockets() OpenApiBuilder().write_marshmallow_schemas_to_openapi_doc() + self.logger.info('Backend has initialized') def _configure_sockets(self) -> None: self.server_socket = socket.socket( @@ -166,8 +174,10 @@ def _configure_sockets(self) -> None: socket.SOL_SOCKET, socket.SO_REUSEADDR, 1, ) - settings = Settings() - server_address = (settings.server_host, settings.server_port) + server_address = ( + self.settings.server_host, + self.settings.server_port, + ) self.server_socket.setblocking(False) self.server_socket.bind(server_address) @@ -182,20 +192,26 @@ async def handle_request(self, client_socket: socket.socket) -> None: message = await handler.handle_request() await self.event_loop.sock_sendall(client_socket, message) + self.logger.info( + f'request from {client_socket.getpeername()} has handled' + ) client_socket.close() async def main(self) -> None: """The method listen server socket for connections, if connection - is gotten, creates client_socket and sends response to it.""" + is gotten, creates client_socket and sends response in it.""" # Getting of event loop in main because it must be in asyncio.run self.event_loop = asyncio.get_event_loop() self.server_socket.listen() while True: - client_socket, client_address = ( + client_socket, _ = ( await self.event_loop.sock_accept(self.server_socket) ) - print(f'get request for connection from {client_address}') + self.logger.info( + f'get request for connection ' + f'from {client_socket.getpeername()}' + ) asyncio.create_task( self.handle_request(client_socket), ) diff --git a/martin_eden/logs.py b/martin_eden/logs.py new file mode 100644 index 0000000..21c5f7b --- /dev/null +++ b/martin_eden/logs.py @@ -0,0 +1,15 @@ +import logging +import sys + + +def configure_logging(level: str = 'DEBUG'): + logging.basicConfig( + level=level, + format=( + '%(asctime)s : %(levelname)s : %(module)s : ' + '%(funcName)s : %(lineno)d : %(message)s' + ), + handlers=[ + logging.StreamHandler(stream=sys.stdout), + ] + ) diff --git a/martin_eden/settings.py b/martin_eden/settings.py index faa0605..bc44371 100644 --- a/martin_eden/settings.py +++ b/martin_eden/settings.py @@ -19,5 +19,5 @@ def read_str(var_name, default=None): class Settings: server_host = read_str('SERVER_HOST') server_port = read_int('SERVER_PORT') - postgres_url = read_str('POSTGRES_URL') + log_level = read_str('LOG_LEVEL') diff --git a/pyproject.toml b/pyproject.toml index c385cbb..265a667 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "martin-eden" -version = "0.10.3" +version = "0.10.4" description = "async backend framework on sockets" authors = ["mrtedn21 "] readme = "README.md"