-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLog.py
More file actions
45 lines (30 loc) · 1003 Bytes
/
Log.py
File metadata and controls
45 lines (30 loc) · 1003 Bytes
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Logger, Utility for logging.
"""
import os
class Logger(object):
def __init__(self, sequence, name):
self.path = os.getcwd()
self.sequence = sequence
self.current_log = self.get_log_path(name)
def get_log_path(self, log_name):
path = os.path.join(self.path, ("{}_" + log_name).format(self.sequence))
log = open(path, 'w')
return log
def log(self, msg):
self.current_log.write(msg)
def fin(self):
self.current_log.flush()
self.current_log.close()
class Singleton(object):
_instance = None
def __new__(cls, *args, **kwargs):
if not isinstance(cls._instance, cls):
cls._instance = object.__new__(cls, *args, **kwargs)
return cls._instance
class LoggerFactory(Singleton, object):
__sequence__ = 0
def get_new_logger(self, log_name):
self.__sequence__ += 1
return Logger(self.__sequence__, log_name)