c2cc265ba8a774b0c012a97533efe060dafa26ce
[yardstick.git] / yardstick / dispatcher / file.py
1 # Copyright 2013 IBM Corp
2 # All Rights Reserved.
3 #
4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
5 #    not use this file except in compliance with the License. You may obtain
6 #    a copy of the License at
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #    Unless required by applicable law or agreed to in writing, software
11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 #    License for the specific language governing permissions and limitations
14 #    under the License.
15
16 # yardstick comment: this is a modified copy of
17 # ceilometer/ceilometer/dispatcher/file.py
18
19 import logging
20 import logging.handlers
21 import json
22
23 from oslo_config import cfg
24
25 from yardstick.dispatcher.base import Base as DispatchBase
26
27 CONF = cfg.CONF
28 OPTS = [
29     cfg.StrOpt('file_path',
30                default='/tmp/yardstick.out',
31                help='Name and the location of the file to record '
32                     'data.'),
33     cfg.IntOpt('max_bytes',
34                default=0,
35                help='The max size of the file.'),
36     cfg.IntOpt('backup_count',
37                default=0,
38                help='The max number of the files to keep.'),
39 ]
40 CONF.register_opts(OPTS, group="dispatcher_file")
41
42
43 class FileDispatcher(DispatchBase):
44     """Dispatcher class for recording data to a file.
45     """
46
47     __dispatcher_type__ = "File"
48
49     def __init__(self, conf):
50         super(FileDispatcher, self).__init__(conf)
51         self.log = None
52
53         # if the directory and path are configured, then log to the file
54         if CONF.dispatcher_file.file_path:
55             dispatcher_logger = logging.Logger('dispatcher.file')
56             dispatcher_logger.setLevel(logging.INFO)
57             # create rotating file handler which logs result
58             rfh = logging.handlers.RotatingFileHandler(
59                 self.conf.get('file_path', CONF.dispatcher_file.file_path),
60                 maxBytes=CONF.dispatcher_file.max_bytes,
61                 backupCount=CONF.dispatcher_file.backup_count,
62                 encoding='utf8')
63
64             rfh.setLevel(logging.INFO)
65             # Only wanted the data to be saved in the file, not the
66             # project root logger.
67             dispatcher_logger.propagate = False
68             dispatcher_logger.addHandler(rfh)
69             self.log = dispatcher_logger
70
71     def record_result_data(self, data):
72         if self.log:
73             self.log.info(json.dumps(data))
74
75     def flush_result_data(self):
76         pass