-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger.py
56 lines (42 loc) · 1.81 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
import logging.handlers
from notifications import NotificationHandler
class Logger:
Logger = None
NotificationHandler = None
def __init__(self, logging_service="crypto_trading", enable_notifications=True):
# Logger setup
self.Logger = logging.getLogger(f"{logging_service}_logger")
self.Logger.setLevel(logging.DEBUG)
self.Logger.propagate = False
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
# default is "logs/crypto_trading.log"
fh = logging.FileHandler(f"./{logging_service}.log")
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
self.Logger.addHandler(fh)
# logging to console
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(formatter)
self.Logger.addHandler(ch)
# notification handler
self.NotificationHandler = NotificationHandler(enable_notifications)
def log(self, message, level="info", notification=True):
if level == "info":
self.Logger.info(message)
elif level == "warning":
self.Logger.warning(message)
elif level == "error":
self.Logger.error(message)
elif level == "debug":
self.Logger.debug(message)
if notification and self.NotificationHandler.enabled:
self.NotificationHandler.send_notification(str(message))
def info(self, message, notification=True):
self.log(message, "info", notification)
def warning(self, message, notification=True):
self.log(message, "warning", notification)
def error(self, message, notification=True):
self.log(message, "error", notification)
def debug(self, message, notification=False):
self.log(message, "debug", notification)