-
Notifications
You must be signed in to change notification settings - Fork 2
/
Logger.py
63 lines (51 loc) · 1.98 KB
/
Logger.py
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
import logging
from config import log_config
class Logging:
def __init__(self, name):
"""
Function is used to instantiate the custom logger
:param name: custom name for the logger
"""
try:
# Creating Custom Logger
self.logger = logging.getLogger(name)
except Exception as e:
raise Exception(e)
def initialize_logger(self):
"""
This function adds the custom formatters and handlers to the logger object
"""
try:
if len(self.logger.handlers) == 0:
# Read the mode
log_level = log_config.log_mode
if log_level == 'ERROR':
self.logger.setLevel(logging.ERROR)
elif log_level == 'DEBUG':
self.logger.setLevel(logging.DEBUG)
# Creating the formatters
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s')
# Creating Handlers
file_handler = logging.FileHandler('Advance Image Downloader.log')
# Adding Formatters to the Handlers
file_handler.setFormatter(formatter)
# Adding Handler to loggers
self.logger.addHandler(file_handler)
return self.logger
except Exception as e:
raise Exception(e)
def print_log(self, log_statement, log_level):
"""
This function is use for printing and logging the statements
:param log_statement: Statement for logging
:param log_level : Level of log that needs to be maintained
"""
try:
if log_level == 'info':
self.logger.info(log_statement)
elif log_level == 'error':
self.logger.error(log_statement)
elif log_level == 'exception':
self.logger.exception(log_statement)
except Exception as e:
raise Exception(e)