Convert to standard logging library

This commit is contained in:
Ian Adam Naval 2015-09-14 23:38:51 -04:00
parent 40a9e0f5f7
commit f4caad52ff

View File

@ -1,4 +1,5 @@
import configparser
import logging
import math
import smtplib
import time
@ -39,7 +40,7 @@ def stdev(s):
def notify_user(username, password, recipient_email_address):
log("Alerting " + recipient_email_address)
logging.info("Alerting " + recipient_email_address)
msg = MIMEText(ALERT_EMAIL_TEXT)
@ -74,8 +75,7 @@ def amplitude_stdev(sliding_window):
def send_notifications(last_notification_sent_at, iftttkey):
seconds_since_last_notification = \
(datetime.now() - last_notification_sent_at).seconds
# Log the time notifications were sent
log("Sending notification after %ds" % seconds_since_last_notification)
logging.info("Sending notification after %ds" % seconds_since_last_notification)
# limit frequency of notifications
if seconds_since_last_notification > MAX_NOTIFICATION_FREQUENCY:
[notify_user(email) for email in RECIPIENT_EMAILS]
@ -88,7 +88,10 @@ def send_notifications(last_notification_sent_at, iftttkey):
def main():
log('started laundry notifier')
LOG_FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(format=LOG_FORMAT)
logging.getLogger().setLevel(logging.INFO)
logging.info('Started laundry notifier')
config = configparser.ConfigParser()
config.read(CONFIG_FILE_PATH)
notifications_section = config['notifications']
@ -116,7 +119,7 @@ def main():
if sliding_stdev['x'] < THRESHOLD:
# Notify recipients on state transitions from 'on' to 'off'
if dryer_state == 'on':
log('Dryer turned off; sliding stdev is %f' % sliding_stdev['x'])
logging.info('Dryer turned off; sliding stdev is %f' % sliding_stdev['x'])
last_notification_sent_at = send_notifications(
last_notification_sent_at,
email_username,
@ -126,15 +129,11 @@ def main():
else:
# Log state transitions from 'off' to 'on'
if dryer_state == 'off':
log('Dryer turned on; sliding stdev is %f' % sliding_stdev['x'])
logging.info('Dryer turned on; sliding stdev is %f' % sliding_stdev['x'])
dryer_state = 'on'
time.sleep(INTERVAL)
def log(message):
print('%s\t%s' % (datetime.isoformat(datetime.now()), message))
if __name__ == '__main__':
main()