forked from edeng23/binance-trade-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
59 lines (45 loc) · 1.6 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
import logging
import logging.handlers
from logging import Handler, Formatter
from notifications import NotificationHandler
LOG_PATH = "crypto_trading.log"
class Logger:
Logger = None
NotificationHandler = None
def __init__(self):
# Logger setup
self.Logger = logging.getLogger("crypto_trader_logger")
self.Logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
fh = logging.FileHandler(LOG_PATH)
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
self.Logger.addHandler(fh)
# logging to console
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
self.Logger.addHandler(ch)
# notification handler
self.NotificationHandler = NotificationHandler()
def log(self, message, level="info", notification=True):
if "info" == level:
self.Logger.info(message)
elif "warning" == level:
self.Logger.warning(message)
elif "error" == level:
self.Logger.error(message)
elif "debug" == level:
self.Logger.debug(message)
if notification and self.NotificationHandler.enabled:
self.NotificationHandler.send_notification(message)
def info(self, message):
self.log(message, "info")
def warning(self, message):
self.log(message, "warning")
def error(self, message):
self.log(message, "error")
def debug(self, message):
self.log(message, "debug")