Simran Basi | 7498d20 | 2012-07-10 15:21:28 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Simran Basi | 4e3d118 | 2013-06-25 16:12:30 -0700 | [diff] [blame^] | 4 | import datetime |
Simran Basi | 7498d20 | 2012-07-10 15:21:28 -0700 | [diff] [blame] | 5 | import logging |
| 6 | import logging.handlers |
Scott Zawalski | 201d6be | 2012-09-21 15:56:25 -0400 | [diff] [blame] | 7 | import os |
Simran Basi | 7498d20 | 2012-07-10 15:21:28 -0700 | [diff] [blame] | 8 | import socket |
| 9 | import time |
| 10 | |
| 11 | from config import rpm_config |
| 12 | |
| 13 | LOGGING_FORMAT = rpm_config.get('GENERAL', 'logging_format') |
Simran Basi | 4e3d118 | 2013-06-25 16:12:30 -0700 | [diff] [blame^] | 14 | RECEIVERS = rpm_config.get('RPM_INFRASTRUCTURE', |
| 15 | 'email_notification_recipients').split(',') |
| 16 | SUBJECT_LINE = (rpm_config.get('GENERAL', 'email_subject_line_format') % |
| 17 | socket.gethostname()) |
| 18 | |
| 19 | |
| 20 | class SuspendableSMTPHandler(logging.handlers.SMTPHandler): |
| 21 | """SMTPHandler that can have it's emails suspended.""" |
| 22 | _suspend_start_time = datetime.datetime.now() |
| 23 | _suspend_time_hrs = 0 |
| 24 | |
| 25 | |
| 26 | def suspend_emails(self, hours): |
| 27 | """Suspend email notifications. |
| 28 | |
| 29 | @param hours: How many hours to suspend email notifications. |
| 30 | """ |
| 31 | self._suspend_start_time = datetime.datetime.now() |
| 32 | self._suspend_time_hrs = int(hours, 0) |
| 33 | |
| 34 | |
| 35 | def resume_emails(self): |
| 36 | """Resume email notifications.""" |
| 37 | self._suspend_time_hrs = 0 |
| 38 | |
| 39 | |
| 40 | def emit(self, record): |
| 41 | """Emit a log record. |
| 42 | |
| 43 | This subclassed version only emits the log record if emails are not |
| 44 | suspended. |
| 45 | |
| 46 | @param record: Log record object we want to emit/record. |
| 47 | """ |
| 48 | if datetime.datetime.now() < (self._suspend_start_time + |
| 49 | datetime.timedelta(hours=self._suspend_time_hrs)): |
| 50 | return |
| 51 | record.msg += ('\n\nTo disable these emails use rpm_client from your ' |
| 52 | 'local checkout. For a 12 hour suspension run: ' |
| 53 | 'site_utils/rpm_control_system/rpm_client.py -d 12') |
| 54 | return super(SuspendableSMTPHandler, self).emit(record) |
Simran Basi | 7498d20 | 2012-07-10 15:21:28 -0700 | [diff] [blame] | 55 | |
| 56 | |
| 57 | def set_up_logging(log_filename_format): |
| 58 | """ |
| 59 | Correctly set up logging to have the correct format/level, log to a file, |
| 60 | and send out email notifications in case of error level messages. |
Simran Basi | 4e3d118 | 2013-06-25 16:12:30 -0700 | [diff] [blame^] | 61 | |
| 62 | @param log_filename_format: Format to use to create the log file. |
| 63 | |
| 64 | @returns email_handler: Logging handler used to send out email alerts. |
Simran Basi | 7498d20 | 2012-07-10 15:21:28 -0700 | [diff] [blame] | 65 | """ |
Scott Zawalski | 201d6be | 2012-09-21 15:56:25 -0400 | [diff] [blame] | 66 | log_filename = os.path.abspath(time.strftime(log_filename_format)) |
| 67 | log_dir = os.path.dirname(log_filename) |
| 68 | if not os.path.isdir(log_dir): |
| 69 | os.makedirs(log_dir) |
Simran Basi | 7498d20 | 2012-07-10 15:21:28 -0700 | [diff] [blame] | 70 | logging.basicConfig(filename=log_filename, level=logging.INFO, |
| 71 | format=LOGGING_FORMAT) |
| 72 | if rpm_config.getboolean('GENERAL', 'debug'): |
| 73 | logging.getLogger().setLevel(logging.DEBUG) |
Simran Basi | 4e3d118 | 2013-06-25 16:12:30 -0700 | [diff] [blame^] | 74 | email_handler = SuspendableSMTPHandler('localhost', 'rpm@google.com', |
| 75 | RECEIVERS, SUBJECT_LINE, None) |
Simran Basi | 7498d20 | 2012-07-10 15:21:28 -0700 | [diff] [blame] | 76 | email_handler.setLevel(logging.ERROR) |
| 77 | email_handler.setFormatter(logging.Formatter(LOGGING_FORMAT)) |
Scott Zawalski | 201d6be | 2012-09-21 15:56:25 -0400 | [diff] [blame] | 78 | logging.getLogger('').addHandler(email_handler) |
Simran Basi | 4e3d118 | 2013-06-25 16:12:30 -0700 | [diff] [blame^] | 79 | return email_handler |