From 2663ed5ae406c0a99a0a610f08f4866d46107eca Mon Sep 17 00:00:00 2001 From: John Nduli Date: Wed, 12 Jun 2024 11:23:47 +0300 Subject: refactor: add db_utils global logger that will be the default --- gn3/db_utils.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/gn3/db_utils.py b/gn3/db_utils.py index e08caf0..19537d4 100644 --- a/gn3/db_utils.py +++ b/gn3/db_utils.py @@ -1,12 +1,15 @@ """module contains all db related stuff""" import contextlib import logging -from typing import Any, Iterator, Optional, Protocol, Tuple +from typing import Any, Iterator, Protocol, Tuple from urllib.parse import urlparse import MySQLdb as mdb import xapian +LOGGER = logging.getLogger(__file__) + + def parse_db_url(sql_uri: str) -> Tuple: """function to parse SQL_URI env variable note:there\ is a default value for SQL_URI so a tuple result is\ @@ -25,10 +28,8 @@ class Connection(Protocol): @contextlib.contextmanager -def database_connection(sql_uri: str, logger: Optional[logging.Logger] = None) -> Iterator[Connection]: +def database_connection(sql_uri: str, logger: logging.Logger = LOGGER) -> Iterator[Connection]: """Connect to MySQL database.""" - if logger is None: - logger = logging.getLogger(__file__) host, user, passwd, db_name, port = parse_db_url(sql_uri) connection = mdb.connect(db=db_name, user=user, -- cgit v1.2.3