Completed
Push — master ( 55df2f...e17347 )
by Daniel
16s queued 12s
created

db_extractor.LoggingNeeds   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A LoggingNeeds.initiate_logger() 0 25 3
1
"""
2
LoggingNeeds
3
4
This library sets all required parameters for a rotating LOG file
5
"""
6
from datetime import datetime
7
# package for Log management
8
import logging
9
import logging.handlers as handlers
10
11
12
class LoggingNeeds:
13
    logger = None
14
15
    def initiate_logger(self, logger_file_name, logger_internal_name):
16
        # initiate Logging
17
        self.logger = logging.getLogger(logger_internal_name)
18
        # set logging level to desired level only if specified
19
        if logger_file_name == 'None':
20
            self.logger.setLevel(logging.NOTSET)
21
        else:
22
            self.logger.setLevel(logging.DEBUG)
23
            # defining log file and setting rotating logic
24
            log_handler = handlers.TimedRotatingFileHandler(logger_file_name,
25
                                                            when='h',
26
                                                            interval=1,
27
                                                            backupCount=5,
28
                                                            encoding='utf-8',
29
                                                            utc=True)
30
            # ensure timestamps are reported as UTC time-zone
31
            my_converter = lambda x, y: datetime.utcnow().timetuple()
32
            logging.Formatter.converter = my_converter
33
            # Here we define our formatter
34
            log_formatter = logging.Formatter(
35
                    '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
36
            # Here we set our logHandler's formatter
37
            log_handler.setFormatter(log_formatter)
38
            # pairing the handler with logging
39
            self.logger.addHandler(log_handler)
40