-
Notifications
You must be signed in to change notification settings - Fork 7
/
logger.py
38 lines (31 loc) · 1.15 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
import logging
import datetime
import random
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class TimeHandler(metaclass=Singleton):
def __init__(self):
self.now = datetime.datetime.now()
self.h_file_name = \
'pipeline-' + \
str(self.now.year) + "-" + \
str(self.now.month) + "-" + \
str(self.now.day) + "--" + \
str(self.now.hour) + "-" + \
str(self.now.minute) + "-" + \
str(self.now.second) + "--" + \
str(random.randint(0, 10000)) + '.log'
self.handler = self.return_handler(self.h_file_name)
@staticmethod
def return_handler(log_name):
# create a file handler
handler = logging.FileHandler(log_name)
handler.setLevel(logging.INFO)
# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
return handler