-
Notifications
You must be signed in to change notification settings - Fork 0
/
pe_logger.py
57 lines (41 loc) · 1.66 KB
/
pe_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
import logging
import os
from typing import Dict
PE_LOG_FILENAME = "pe_log.txt"
PE_LOG_DIR = "generated"
PE_DEBUG_LEVEL = logging.DEBUG
class LoggerSingleton:
"""Singleton class to have only one logger instance
See: https://stackoverflow.com/questions/6760685/creating-a-singleton-in-python
Returns:
descendant of LoggerSingleton: Singleton instance of subclass
"""
_instances: Dict[type, object] = {}
def __new__(class_, *args, **kwargs):
if class_ not in class_._instances:
class_._instances[class_] = super(LoggerSingleton, class_).__new__(
class_, *args, **kwargs
)
return class_._instances[class_]
class PELogger(LoggerSingleton):
logger = None
def get_logger(self):
if self.logger is None:
os.makedirs(PE_LOG_DIR, exist_ok=True)
self.logger = logging.getLogger()
# create a file handler
file_handler = logging.FileHandler(
filename=os.path.join(PE_LOG_DIR, PE_LOG_FILENAME), mode="a"
)
file_handler.setLevel(PE_DEBUG_LEVEL)
console_handler = logging.StreamHandler()
console_handler.setLevel(PE_DEBUG_LEVEL)
# create a logging format
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
# add the handlers to the logger
self.logger.addHandler(file_handler)
self.logger.addHandler(console_handler)
self.logger.setLevel(PE_DEBUG_LEVEL)
return self.logger