-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
83 lines (64 loc) · 2.24 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
import logging
import os
import datetime
from collections import defaultdict
import time
import numpy as np
class SingletonType(type):
"""
Singleton class to create a singleton logger
"""
_instances = {}
def __call__(cls, *args):
cls_id = str(cls)
if len(args) > 0:
cls_id += str(args[0])
if cls_id not in cls._instances:
cls._instances[cls_id] = super(SingletonType, cls).__call__(*args)
return cls._instances[cls_id]
class Logger(object, metaclass=SingletonType):
def __init__(self, storage_parent_folder, debug=True):
self._logger = logging.getLogger("pgdl")
self.debug_param = debug
if debug:
self._logger.setLevel(logging.DEBUG)
else:
self._logger.setLevel(logging.WARNING)
self.storage_parent_folder = storage_parent_folder
formatter = logging.Formatter(
"%(asctime)s \t [%(levelname)s | %(filename)s:%(lineno)s] > %(message)s"
)
now = datetime.datetime.now()
if not os.path.isdir(self.storage_parent_folder):
os.makedirs(self.storage_parent_folder)
fileHandler = logging.FileHandler(
os.path.join(
self.storage_parent_folder, "log_" + now.strftime("%Y-%m-%d") + ".log"
)
)
streamHandler = logging.StreamHandler()
fileHandler.setFormatter(formatter)
streamHandler.setFormatter(formatter)
if not (self._logger.hasHandlers()):
self._logger.addHandler(fileHandler)
self._logger.addHandler(streamHandler)
def get_logger(self):
return self
def info(self, message):
self._logger.info(message)
def debug(self, message):
self._logger.debug(message)
def warning(self, message):
self._logger.warning(message)
def error(self, message):
self._logger.error(message)
def exception(self, message):
self._logger.exception("[Exception]" + message)
def critical(self, message):
self._logger.critical(message)
# a simple usecase
if __name__ == "__main__":
logger = Logger.__call__("test").get_logger()
logger.info("Hello, Logger")
logger.debug("bug occured")