mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-11-30 13:20:40 +03:00
logger refactor
This commit is contained in:
0
app/utils/__init__.py
Normal file
0
app/utils/__init__.py
Normal file
22
app/utils/logging.py
Normal file
22
app/utils/logging.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import logging
|
||||
|
||||
from rich.console import Console
|
||||
from rich.logging import RichHandler
|
||||
|
||||
|
||||
from app.utils.singleton import SingletonMeta
|
||||
|
||||
|
||||
class AppLogger(metaclass=SingletonMeta):
|
||||
_logger = None
|
||||
|
||||
def __init__(self):
|
||||
self._logger = logging.getLogger(__name__)
|
||||
|
||||
def get_logger(self):
|
||||
return self._logger
|
||||
|
||||
|
||||
class RichConsoleHandler(RichHandler):
|
||||
def __init__(self, width=200, style=None, **kwargs):
|
||||
super().__init__(console=Console(color_system="256", width=width, style=style), **kwargs)
|
||||
18
app/utils/singleton.py
Normal file
18
app/utils/singleton.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from threading import Lock
|
||||
|
||||
|
||||
class SingletonMeta(type):
|
||||
"""
|
||||
This is a thread-safe implementation of Singleton.
|
||||
"""
|
||||
|
||||
_instances = {}
|
||||
|
||||
_lock: Lock = Lock()
|
||||
|
||||
def __call__(cls, *args, **kwargs):
|
||||
with cls._lock:
|
||||
if cls not in cls._instances:
|
||||
instance = super().__call__(*args, **kwargs)
|
||||
cls._instances[cls] = instance
|
||||
return cls._instances[cls]
|
||||
Reference in New Issue
Block a user