1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 # liangqi1@huawei.com matthew.lijun@huawei.com
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
13 from oslo_config import cfg
15 from utils.dispatcher.base import Base as DispatchBase
19 cfg.StrOpt('file_path',
20 default='/tmp/bottlenecks.out',
21 help='Name and the location of the file to record '
23 cfg.IntOpt('max_bytes',
25 help='The max size of the file.'),
26 cfg.IntOpt('backup_count',
28 help='The max number of the files to keep.'),
30 CONF.register_opts(opts, group="dispatcher_file")
33 class FileDispatcher(DispatchBase):
34 """Dispatcher class for recording data to a file.
37 __dispatcher_type__ = "File"
39 def __init__(self, conf):
40 super(FileDispatcher, self).__init__(conf)
43 # if the directory and path are configured, then log to the file
44 if CONF.dispatcher_file.file_path:
45 dispatcher_logger = logging.Logger('dispatcher.file')
46 dispatcher_logger.setLevel(logging.INFO)
47 # create rotating file handler which logs result
48 rfh = logging.handlers.RotatingFileHandler(
49 self.conf.get('file_path', CONF.dispatcher_file.file_path),
50 maxBytes=CONF.dispatcher_file.max_bytes,
51 backupCount=CONF.dispatcher_file.backup_count,
54 rfh.setLevel(logging.INFO)
55 # Only wanted the data to be saved in the file, not the
56 # project root logger.
57 dispatcher_logger.propagate = False
58 dispatcher_logger.addHandler(rfh)
59 self.log = dispatcher_logger
61 def record_result_data(self, data):
63 self.log.info(json.dumps(data))
65 def flush_result_data(self):