Skip to content

Commit

Permalink
add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtedn21 committed Nov 21, 2023
1 parent 30f74f0 commit daf8664
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 21 additions & 5 deletions martin_eden/core.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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)

Expand All @@ -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),
)
15 changes: 15 additions & 0 deletions martin_eden/logs.py
Original file line number Diff line number Diff line change
@@ -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),
]
)
2 changes: 1 addition & 1 deletion martin_eden/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit daf8664

Please sign in to comment.