-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.py
65 lines (51 loc) · 2.28 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
60
61
62
63
64
65
from datetime import datetime, timedelta
import logging
import logging.handlers
import os
from mcu_watchdog import conf
################################################################################
# EMAIL Filter
# To avoid email spam, sent error email 1 per hour (to warn administrators, who can check and fix the problem)
class ErrorEmailFilter(logging.Filter):
def filter(self, record):
# check if the level is error
if record.levelno == logging.ERROR:
last_sent = None
filepath = os.path.join('temp', 'last_email_sent.txt')
now = datetime.now()
# save locally in a file last time email was sent
if os.path.exists(filepath):
# read the configuration
fp = open(filepath)
date_read = fp.readline()
last_sent = datetime.strptime(date_read, '%Y-%m-%d %H:%M:%S')
fp.close()
# if time expired, send error email
result = not last_sent or (now - last_sent) > timedelta(minutes=conf.LOG_EMAIL_FREQUENCY)
if result:
# update last sent email
fp = open(filepath, 'w')
fp.write(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
fp.close()
return result
return True
# create logger
logger = logging.getLogger("MCU")
logger.setLevel(conf.LOG_LEVEL)
formatter = logging.Formatter('%(asctime)s | %(levelname)s | %(message)s')
formatter_error = logging.Formatter('****** ERROR ****** %(asctime)s | %(module)s/%(filename)s | %(funcName)s():%(lineno)d | %(message)s')
file_handler = logging.FileHandler(conf.LOG_FILEPATH)
file_error_handler = logging.FileHandler(conf.LOG_ERROR_FILEPATH)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
file_error_handler.setLevel(logging.ERROR)
file_error_handler.setFormatter(formatter_error)
logger.addHandler(file_error_handler)
if conf.LOG_EMAIL:
# email in case of errors
mail_handler = logging.handlers.SMTPHandler(conf.LOG_MAIL_HOSTNAME, conf.LOG_MAIL_FROM, conf.LOG_MAIL_TO, "[MCU] Error")
mail_handler.setLevel(logging.ERROR)
mail_handler.setFormatter(formatter_error)
logger.addFilter(ErrorEmailFilter())
logger.addHandler(mail_handler)