Blame view

src/utils/Logger.py 1.18 KB
d91a4c2b   Jeremy   Inserted logger
1
2
from django.conf import settings
import logging
d91a4c2b   Jeremy   Inserted logger
3

b5e9fde1   Jeremy   Update django ver...
4
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)
d91a4c2b   Jeremy   Inserted logger
5

d91a4c2b   Jeremy   Inserted logger
6
7
def setupLogger(logger_name, log_file, level=logging.INFO):
    l = logging.getLogger(logger_name)
b5e9fde1   Jeremy   Update django ver...
8
    formatter = logging.Formatter('[%(message)s] at %(filename)s : %(lineno)s')
d91a4c2b   Jeremy   Inserted logger
9
10
11
12
13
14
15
16
17
    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))
bca9a283   Jeremy   Reworked the sche...
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35


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)