-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.py
28 lines (24 loc) · 983 Bytes
/
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
import datetime
import logging
import os
import sys
LOGS_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
class Logger(logging.Logger):
def __init__(self, name="logger", consoleOut=False):
super().__init__(name)
self.setLevel(logging.DEBUG)
self.formatter = logging.Formatter(
"%(asctime)s.%(msecs)03d | %(levelname)s | [%(module)s] %(message)s",
datefmt="%H:%M:%S",
)
now = datetime.datetime.now()
self.outputFile = f"{LOGS_PATH}/{self.name}_{now.strftime('%Y-%m-%d')}.log"
if not os.path.exists(LOGS_PATH):
os.makedirs(LOGS_PATH)
fileHandler = logging.FileHandler(self.outputFile)
fileHandler.setFormatter(self.formatter)
self.addHandler(fileHandler)
if consoleOut:
streamHandler = logging.StreamHandler(sys.stdout)
streamHandler.setFormatter(self.formatter)
self.addHandler(streamHandler)