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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 Centralized logging configuration and formatting.
23 from logutils import dictconfig
30 LOGGER_CONFIG_TEMPLATE = {
34 "format": "%(asctime)s [%(levelname)s] %(message)s"
37 "format": "%(message)s"
42 "class": "logging.handlers.RotatingFileHandler",
44 "maxBytes": "5000000",
48 "class": "logging.StreamHandler",
49 "stream": "ext://sys.stdout",
50 "formatter": "console"
53 "disable_existing_loggers": False
57 class Logging(object):
59 def __init__(self, config):
61 self._verbosity_level = NO_VERBOSE
62 self._all_loggers_names = []
63 self._configure_loggers(config)
64 self._lgr = logging.getLogger('aria.cli.main')
75 def verbosity_level(self):
76 return self._verbosity_level
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)
85 def is_high_verbose_level(self):
86 return self.verbosity_level == HIGH_VERBOSE
88 def _configure_loggers(self, config):
89 loggers_config = config.logging.loggers
90 logfile = config.logging.filename
92 logger_dict = copy.deepcopy(LOGGER_CONFIG_TEMPLATE)
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
101 del logger_dict['handlers']['file']
103 # add handlers to all 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
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()]
116 dictconfig.dictConfig(logger_dict)
119 class ModelLogIterator(object):
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 {}
129 filters = dict(execution_fk=self._execution_id, id=dict(gt=self._last_visited_id))
130 filters.update(self._additional_filters)
132 for log in self._model_storage.log.iter(filters=filters, sort=self._sort):
133 self._last_visited_id = log.id