Spaces:
Running
on
Zero
Running
on
Zero
File size: 2,094 Bytes
aafe7f2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import logging
from functools import wraps
from threading import Lock
class Logger:
_instances = {}
_lock = Lock()
def __new__(cls, name="root"):
with cls._lock:
if name not in cls._instances:
instance = super().__new__(cls)
instance._init(name)
cls._instances[name] = instance
return cls._instances[name]
def _init(self, name):
self.logger = logging.getLogger(name)
self.logger.setLevel(logging.DEBUG)
self.logger.propagate = False
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
file_handler = logging.FileHandler("app.log")
file_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter(
"%(asctime)s [%(threadName)s] %(levelname)-5s %(name)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S", # no milliseconds
)
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
self.logger.addHandler(console_handler)
self.logger.addHandler(file_handler)
def _log(self, level, message):
log_message = f"{message}".strip()
self.logger.log(level, log_message)
def debug(self, message, **kwargs):
self._log(logging.DEBUG, message, **kwargs)
def info(self, message, **kwargs):
self._log(logging.INFO, message, **kwargs)
def warning(self, message, **kwargs):
self._log(logging.WARNING, message, **kwargs)
def error(self, message, **kwargs):
self._log(logging.ERROR, message, **kwargs)
def critical(self, message, **kwargs):
self._log(logging.CRITICAL, message, **kwargs)
# decorator for logging function calls
def log_fn(name=None):
def decorator(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
log = Logger(name or fn.__name__)
log.info("begin")
result = fn(*args, **kwargs)
log.info("end")
return result
return wrapper
return decorator
|