-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.py
66 lines (54 loc) · 1.67 KB
/
common.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
import importlib
import logging.config
import os
from alert import EmailAlert
from config import LOG_LEVEL, SEND_EMAILS
__all__ = ["notify_error"]
LOGGING_CONFIG = {
"version": 1,
"formatters": {"standard": {"format": "%(asctime)s - %(levelname)s - %(message)s"}},
"handlers": {
"default": {
"level": "DEBUG",
"formatter": "standard",
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
},
"file": {
"class": "logging.handlers.RotatingFileHandler",
"level": "INFO",
"formatter": "standard",
"filename": "backup.log",
"maxBytes": 500000,
"backupCount": 15,
},
},
"loggers": {"": {"level": LOG_LEVEL, "handlers": ["default", "file"]}},
}
logging.config.dictConfig(LOGGING_CONFIG)
def notify_error(error: str = "") -> None:
if not SEND_EMAILS:
return
EmailAlert().send(
os.getenv("EMAIL_SUBJECT"),
os.getenv("EMAIL_BODY") + f"\n\n{error}",
os.getenv("TO_EMAIL"),
)
def _check_required(name: str) -> None:
mod = importlib.import_module("config")
if name not in mod or not mod[name]:
logging.getLogger(__name__).error(
f"{name} environment variable missing. Please set it in your .env file."
)
notify_error()
exit(1)
_check_required("API_KEY")
_check_required("GET_URL")
_check_required("SERVERS_URL")
if SEND_EMAILS:
_check_required("FROM_EMAIL")
_check_required("SMTP_SERVER")
_check_required("SMTP_PORT")
_check_required("EMAIL_SUBJECT")
_check_required("EMAIL_BODY")
_check_required("TO_EMAIL")