Merge "Change PTL informatin in INFO"
[bottlenecks.git] / utils / dispatcher / file.py
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 ##############################################################################
9
10 import logging
11 import json
12
13 from oslo_config import cfg
14
15 from utils.dispatcher.base import Base as DispatchBase
16
17 CONF = cfg.CONF
18 opts = [
19     cfg.StrOpt('file_path',
20                default='/tmp/bottlenecks.out',
21                help='Name and the location of the file to record '
22                     'data.'),
23     cfg.IntOpt('max_bytes',
24                default=0,
25                help='The max size of the file.'),
26     cfg.IntOpt('backup_count',
27                default=0,
28                help='The max number of the files to keep.'),
29 ]
30 CONF.register_opts(opts, group="dispatcher_file")
31
32
33 class FileDispatcher(DispatchBase):
34     """Dispatcher class for recording data to a file.
35     """
36
37     __dispatcher_type__ = "File"
38
39     def __init__(self, conf):
40         super(FileDispatcher, self).__init__(conf)
41         self.log = None
42
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,
52                 encoding='utf8')
53
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
60
61     def record_result_data(self, data):
62         if self.log:
63             self.log.info(json.dumps(data))
64
65     def flush_result_data(self):
66         pass