blob: e887f55a8db9b3453b5d32e29ebc432e53aa82af [file] [log] [blame]
Simran Basi7498d202012-07-10 15:21:28 -07001# 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 Basi4e3d1182013-06-25 16:12:30 -07004import datetime
Simran Basi7498d202012-07-10 15:21:28 -07005import logging
6import logging.handlers
Scott Zawalski201d6be2012-09-21 15:56:25 -04007import os
Simran Basi7498d202012-07-10 15:21:28 -07008import socket
9import time
10
11from config import rpm_config
12
13LOGGING_FORMAT = rpm_config.get('GENERAL', 'logging_format')
Simran Basi4e3d1182013-06-25 16:12:30 -070014RECEIVERS = rpm_config.get('RPM_INFRASTRUCTURE',
15 'email_notification_recipients').split(',')
16SUBJECT_LINE = (rpm_config.get('GENERAL', 'email_subject_line_format') %
17 socket.gethostname())
18
19
20class 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 Basi7498d202012-07-10 15:21:28 -070055
56
57def 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 Basi4e3d1182013-06-25 16:12:30 -070061
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 Basi7498d202012-07-10 15:21:28 -070065 """
Scott Zawalski201d6be2012-09-21 15:56:25 -040066 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 Basi7498d202012-07-10 15:21:28 -070070 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 Basi4e3d1182013-06-25 16:12:30 -070074 email_handler = SuspendableSMTPHandler('localhost', 'rpm@google.com',
75 RECEIVERS, SUBJECT_LINE, None)
Simran Basi7498d202012-07-10 15:21:28 -070076 email_handler.setLevel(logging.ERROR)
77 email_handler.setFormatter(logging.Formatter(LOGGING_FORMAT))
Scott Zawalski201d6be2012-09-21 15:56:25 -040078 logging.getLogger('').addHandler(email_handler)
Simran Basi4e3d1182013-06-25 16:12:30 -070079 return email_handler