1#!/usr/bin/env python
2#
3#   Copyright 2017 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17import logging
18
19from reporters.reporter import Reporter
20
21
22class LoggerReporter(Reporter):
23    def report(self, metric_responses):
24        # Extra formatter options.
25        extra = {
26            'metric_name': None,
27            'response_key': None,
28            'response_val': None
29        }
30
31        logger = logging.getLogger(__name__)
32        logger.setLevel(logging.INFO)
33        # Stop logger from print to stdout.
34        logger.propagate = False
35
36        handler = logging.FileHandler('lab_health.log')
37        handler.setLevel(logging.INFO)
38
39        formatter = logging.Formatter(
40            '%(asctime)s: %(metric_name)s (%(response_key)s %(response_val)s)')
41        handler.setFormatter(formatter)
42        logger.addHandler(handler)
43
44        logger = logging.LoggerAdapter(logger, extra)
45        # add the handlers to the logger
46        for metric in metric_responses:
47            extra['metric_name'] = metric
48            for response in metric_responses[metric]:
49                extra['response_key'] = response
50                extra['response_val'] = metric_responses[metric][response]
51                logger.info(None)
52