vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / cli / logger.py
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License.  You may obtain 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,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """
17 Centralized logging configuration and formatting.
18 """
19
20 import os
21 import copy
22 import logging
23 from logutils import dictconfig
24
25 HIGH_VERBOSE = 3
26 MEDIUM_VERBOSE = 2
27 LOW_VERBOSE = 1
28 NO_VERBOSE = 0
29
30 LOGGER_CONFIG_TEMPLATE = {
31     "version": 1,
32     "formatters": {
33         "file": {
34             "format": "%(asctime)s [%(levelname)s] %(message)s"
35         },
36         "console": {
37             "format": "%(message)s"
38         }
39     },
40     "handlers": {
41         "file": {
42             "class": "logging.handlers.RotatingFileHandler",
43             "formatter": "file",
44             "maxBytes": "5000000",
45             "backupCount": "20"
46         },
47         "console": {
48             "class": "logging.StreamHandler",
49             "stream": "ext://sys.stdout",
50             "formatter": "console"
51         }
52     },
53     "disable_existing_loggers": False
54 }
55
56
57 class Logging(object):
58
59     def __init__(self, config):
60         self._log_file = None
61         self._verbosity_level = NO_VERBOSE
62         self._all_loggers_names = []
63         self._configure_loggers(config)
64         self._lgr = logging.getLogger('aria.cli.main')
65
66     @property
67     def logger(self):
68         return self._lgr
69
70     @property
71     def log_file(self):
72         return self._log_file
73
74     @property
75     def verbosity_level(self):
76         return self._verbosity_level
77
78     @verbosity_level.setter
79     def verbosity_level(self, level):
80         self._verbosity_level = level
81         if self.is_high_verbose_level():
82             for logger_name in self._all_loggers_names:
83                 logging.getLogger(logger_name).setLevel(logging.DEBUG)
84
85     def is_high_verbose_level(self):
86         return self.verbosity_level == HIGH_VERBOSE
87
88     def _configure_loggers(self, config):
89         loggers_config = config.logging.loggers
90         logfile = config.logging.filename
91
92         logger_dict = copy.deepcopy(LOGGER_CONFIG_TEMPLATE)
93         if logfile:
94             # set filename on file handler
95             logger_dict['handlers']['file']['filename'] = logfile
96             logfile_dir = os.path.dirname(logfile)
97             if not os.path.exists(logfile_dir):
98                 os.makedirs(logfile_dir)
99             self._log_file = logfile
100         else:
101             del logger_dict['handlers']['file']
102
103         # add handlers to all loggers
104         loggers = {}
105         for logger_name in loggers_config:
106             loggers[logger_name] = dict(handlers=list(logger_dict['handlers'].keys()))
107             self._all_loggers_names.append(logger_name)
108         logger_dict['loggers'] = loggers
109
110         # set level for all loggers
111         for logger_name, logging_level in loggers_config.iteritems():
112             log = logging.getLogger(logger_name)
113             level = logging._levelNames[logging_level.upper()]
114             log.setLevel(level)
115
116         dictconfig.dictConfig(logger_dict)
117
118
119 class ModelLogIterator(object):
120
121     def __init__(self, model_storage, execution_id, filters=None, sort=None, offset=0):
122         self._last_visited_id = offset
123         self._model_storage = model_storage
124         self._execution_id = execution_id
125         self._additional_filters = filters or {}
126         self._sort = sort or {}
127
128     def __iter__(self):
129         filters = dict(execution_fk=self._execution_id, id=dict(gt=self._last_visited_id))
130         filters.update(self._additional_filters)
131
132         for log in self._model_storage.log.iter(filters=filters, sort=self._sort):
133             self._last_visited_id = log.id
134             yield log