Browse Source

Convert to standard logging library

Ian Adam Naval 4 years ago
parent
commit
f4caad52ff
1 changed files with 9 additions and 10 deletions
  1. 9
    10
      laundry_notifier/run.py

+ 9
- 10
laundry_notifier/run.py View File

@@ -1,4 +1,5 @@
1 1
 import configparser
2
+import logging
2 3
 import math
3 4
 import smtplib
4 5
 import time
@@ -39,7 +40,7 @@ def stdev(s):
39 40
 
40 41
 
41 42
 def notify_user(username, password, recipient_email_address):
42
-    log("Alerting " + recipient_email_address)
43
+    logging.info("Alerting " + recipient_email_address)
43 44
 
44 45
     msg = MIMEText(ALERT_EMAIL_TEXT)
45 46
 
@@ -74,8 +75,7 @@ def amplitude_stdev(sliding_window):
74 75
 def send_notifications(last_notification_sent_at, iftttkey):
75 76
     seconds_since_last_notification = \
76 77
         (datetime.now() - last_notification_sent_at).seconds
77
-    # Log the time notifications were sent
78
-    log("Sending notification after %ds" % seconds_since_last_notification)
78
+    logging.info("Sending notification after %ds" % seconds_since_last_notification)
79 79
     # limit frequency of notifications
80 80
     if seconds_since_last_notification > MAX_NOTIFICATION_FREQUENCY:
81 81
         [notify_user(email) for email in RECIPIENT_EMAILS]
@@ -88,7 +88,10 @@ def send_notifications(last_notification_sent_at, iftttkey):
88 88
 
89 89
 
90 90
 def main():
91
-    log('started laundry notifier')
91
+    LOG_FORMAT = '%(asctime)-15s %(message)s'
92
+    logging.basicConfig(format=LOG_FORMAT)
93
+    logging.getLogger().setLevel(logging.INFO)
94
+    logging.info('Started laundry notifier')
92 95
     config = configparser.ConfigParser()
93 96
     config.read(CONFIG_FILE_PATH)
94 97
     notifications_section = config['notifications']
@@ -116,7 +119,7 @@ def main():
116 119
                 if sliding_stdev['x'] < THRESHOLD:
117 120
                     # Notify recipients on state transitions from 'on' to 'off'
118 121
                     if dryer_state == 'on':
119
-                        log('Dryer turned off; sliding stdev is %f' % sliding_stdev['x'])
122
+                        logging.info('Dryer turned off; sliding stdev is %f' % sliding_stdev['x'])
120 123
                         last_notification_sent_at = send_notifications(
121 124
                             last_notification_sent_at,
122 125
                             email_username,
@@ -126,15 +129,11 @@ def main():
126 129
                 else:
127 130
                     # Log state transitions from 'off' to 'on'
128 131
                     if dryer_state == 'off':
129
-                        log('Dryer turned on; sliding stdev is %f' % sliding_stdev['x'])
132
+                        logging.info('Dryer turned on; sliding stdev is %f' % sliding_stdev['x'])
130 133
                     dryer_state = 'on'
131 134
 
132 135
             time.sleep(INTERVAL)
133 136
 
134 137
 
135
-def log(message):
136
-    print('%s\t%s' % (datetime.isoformat(datetime.now()), message))
137
-
138
-
139 138
 if __name__ == '__main__':
140 139
     main()

Loading…
Cancel
Save