-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.py
48 lines (37 loc) · 1.43 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
import os
import logging
from time import strftime, gmtime
from __init__ import LOGGING_DIR
class Logger:
"""
Creates and initializes a logger using the `logging` Python module
"""
# TODO: Modify filename to *.log
def __init__(self, name, filename="log.txt"):
"""
Initializes the logger
:param filename:
"""
self.timestamp = strftime("%Y_%m_%d_%H_%M_%S", gmtime())
self.filename = f'{name}_{self.timestamp}_{filename}'
self.name = name
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%m-%d-%Y %H:%M',
filename=os.path.join(LOGGING_DIR, self.filename),
filemode='a')
# Define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
# Set a format which is simpler for console use
formatter = logging.Formatter('%(message)s')
# Tell the handler to use this format
console.setFormatter(formatter)
# Add the handler to the root logger
logging.getLogger('').addHandler(console)
self.logger_object = logging.getLogger(name)
def get_logger(self):
"""
:return: the logger object
"""
return self.logger_object