Simran Basi | 93a2cd6 | 2013-10-23 13:29:28 -0700 | [diff] [blame] | 1 | # Copyright (c) 2013 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. |
| 4 | import logging |
| 5 | import time |
| 6 | |
| 7 | import common |
| 8 | |
| 9 | from autotest_lib.client.common_lib import logging_config |
| 10 | |
| 11 | |
| 12 | class DroneLoggingConfig(logging_config.LoggingConfig): |
| 13 | """This class sets up logging for the Drone Machines. |
| 14 | |
| 15 | Drone_utility is kicked off on each tick, so this logging config sets up |
| 16 | the log file to timestamp by day and will create a daily log file. |
| 17 | """ |
| 18 | |
| 19 | |
| 20 | @classmethod |
| 21 | def get_timestamped_log_name(cls, base_name): |
| 22 | """Generate a log file name based off of Today's Date. |
| 23 | |
| 24 | Normally the other processes in the infrastructure (like the scheduler) |
| 25 | are kicked off once for long periods of time. However drone_utility is |
| 26 | kicked off once per tick. Therefore get_timestamped_log_name is |
| 27 | overloaded so the returned log name just includes the current date. |
| 28 | |
| 29 | @param base_name: String to start the log's filename with. |
| 30 | |
| 31 | @returns String of the base_name suffixed with a timestamp of today's |
| 32 | date. |
| 33 | """ |
| 34 | return '%s.log.%s' % (base_name, time.strftime('%Y-%m-%d')) |
| 35 | |
| 36 | |
| 37 | def configure_logging(self, log_dir=None, logfile_name=None): |
| 38 | """Configure logging for the Drones. |
| 39 | |
| 40 | If log_dir and logfile_name are not provided, it will request a |
| 41 | timestamped log name with prefix 'drone'. Both the stdout and stderr |
| 42 | logging handlers are disabled because drone_utility's output is parsed |
| 43 | by the caller. |
| 44 | |
| 45 | This function is called by client/common_lib/logging_manager.py which |
| 46 | manages a logging_config. For example if any modules want to adjust |
| 47 | logging (enabling and/or disabling loggers) after drone_utility has |
| 48 | started they will do so through the logging_manager. |
| 49 | |
| 50 | @param log_dir: Directory to store the log in. If none will use |
| 51 | /usr/local/autotest/logs |
| 52 | @param logfile_name: Name of the log file. If none it will be in the |
| 53 | format of 'drone.log.YEAR-MONTH-DAY' |
| 54 | |
| 55 | """ |
| 56 | # Disable the default stdout/stderr handlers. |
| 57 | self._clear_all_handlers() |
| 58 | if log_dir is None: |
| 59 | log_dir = self.get_server_log_dir() |
| 60 | if not logfile_name: |
| 61 | logfile_name = self.get_timestamped_log_name('drone') |
| 62 | |
| 63 | for level in (logging.DEBUG, logging.INFO, logging.WARNING, |
| 64 | logging.ERROR, logging.CRITICAL): |
| 65 | self.add_file_handler(logfile_name, level, log_dir=log_dir) |