mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
add singleton design pattern
This commit is contained in:
parent
525c8c94b2
commit
8af785be98
35
app/utils.py
35
app/utils.py
@ -1,21 +1,18 @@
|
|||||||
import logging
|
from threading import Lock
|
||||||
from functools import lru_cache
|
|
||||||
|
|
||||||
from rich.console import Console
|
|
||||||
from rich.logging import RichHandler
|
|
||||||
|
|
||||||
console = Console(color_system="256", width=200, style="blue")
|
|
||||||
|
|
||||||
|
|
||||||
@lru_cache
|
class SingletonMeta(type):
|
||||||
def get_logger(module_name):
|
"""
|
||||||
logger = logging.getLogger(module_name)
|
This is a thread-safe implementation of Singleton.
|
||||||
handler = RichHandler(
|
"""
|
||||||
rich_tracebacks=True, console=console, tracebacks_show_locals=True
|
|
||||||
)
|
_instances = {}
|
||||||
handler.setFormatter(
|
|
||||||
logging.Formatter("[ %(threadName)s:%(funcName)s:%(lineno)d ] - %(message)s")
|
_lock: Lock = Lock()
|
||||||
)
|
|
||||||
logger.addHandler(handler)
|
def __call__(cls, *args, **kwargs):
|
||||||
logger.setLevel(logging.DEBUG)
|
with cls._lock:
|
||||||
return logger
|
if cls not in cls._instances:
|
||||||
|
instance = super().__call__(*args, **kwargs)
|
||||||
|
cls._instances[cls] = instance
|
||||||
|
return cls._instances[cls]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user