Blame view

src/core/pyros_django/utils/Logger.py 1.9 KB
1cffbf1c   Etienne Pallier   moved pyros_djang...
1
2
3
from django.conf import settings
import logging

ad6d274a   Etienne Pallier   Restructuration d...
4
5
6
7
8
9
10
11
12
# If django project is in /src/ :
#RELATIVE_PATH_TO_LOG_DIR = '../logs/'
#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)
# If django project is in /src/core/pyros_django/ : 
RELATIVE_PATH_TO_LOG_DIR = '../../../logs'
logging.basicConfig(filename = f'{settings.BASE_DIR}/{RELATIVE_PATH_TO_LOG_DIR}/pyros.log', 
                    format = '-> At : [%(asctime)s]\n\t By module [%(module)s] logger : [%(name)s] : "%(message)s"', 
                    level = logging.DEBUG)
#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)
1cffbf1c   Etienne Pallier   moved pyros_djang...
13
14
15
16

def setupLogger(logger_name, log_file, level=logging.INFO):
    l = logging.getLogger(logger_name)
    formatter = logging.Formatter('[%(message)s] at %(filename)s : %(lineno)s')
ad6d274a   Etienne Pallier   Restructuration d...
17
18
    #fileHandler = logging.FileHandler('%s/../logs/%s.log'%(settings.BASE_DIR, log_file), mode='w')
    fileHandler = logging.FileHandler(f'{settings.BASE_DIR}/{RELATIVE_PATH_TO_LOG_DIR}/{log_file}.log', mode='w')
1cffbf1c   Etienne Pallier   moved pyros_djang...
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
    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)