Logger.py 1.18 KB
from django.conf import settings
import logging

logging.basicConfig(filename='%s/../logs/pyros.log'%(settings.BASE_DIR), format='-> At : [%(asctime)s]\n\t By module [%(module)s] logger : [%(name)s] : "%(message)s"', level=logging.DEBUG)

def setupLogger(logger_name, log_file, level=logging.INFO):
    l = logging.getLogger(logger_name)
    formatter = logging.Formatter('[%(message)s] at %(filename)s : %(lineno)s')
    fileHandler = logging.FileHandler('%s/../logs/%s.log'%(settings.BASE_DIR, log_file), mode='w')
    fileHandler.setFormatter(formatter)
    # streamHandler = logging.StreamHandler()
    # streamHandler.setFormatter(formatter)

    l.setLevel(level)
    l.addHandler(fileHandler)
    # l.addHandler(streamHandler)
    return (logging.getLogger(logger_name))


class Logger:
    def __init__(self, name: str, file: str):
        super().__init__()
        self.logger = setupLogger(name, file)

    def log(self, message):
        if settings.DEBUG:
            self.logger.info(message)

    def info(self, message: str):
        if settings.DEBUG:
            self.logger.info(message)

    def debug(self, message: str):
        if settings.DEBUG:
            self.logger.debug(message)