|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +# |
| 13 | + |
| 14 | +"""Context and Formatter""" |
| 15 | + |
| 16 | +import logging |
| 17 | + |
| 18 | +_LOG_MESSAGE_FORMAT = ('%(asctime)s.%(msecs)03d %(process)d ' |
| 19 | + '%(levelname)s %(name)s [%(clouds_name)s ' |
| 20 | + '%(username)s %(project_name)s] %(message)s') |
| 21 | +_LOG_DATE_FORMAT = '%Y-%m-%d %H:%M:%S' |
| 22 | + |
| 23 | + |
| 24 | +def setup_handler_logging_level(handler_type, level): |
| 25 | + """Setup of the handler for set the logging level |
| 26 | +
|
| 27 | + :param handler_type: type of logging handler |
| 28 | + :param level: logging level |
| 29 | + :return: None |
| 30 | + """ |
| 31 | + # Set the handler logging level of FileHandler(--log-file) |
| 32 | + # and StreamHandler |
| 33 | + for h in logging.getLogger('').handlers: |
| 34 | + if type(h) is handler_type: |
| 35 | + h.setLevel(level) |
| 36 | + |
| 37 | + |
| 38 | +def setup_logging(shell, cloud_config): |
| 39 | + """Get one cloud configuration from configuration file and setup logging |
| 40 | +
|
| 41 | + :param shell: instance of openstackclient shell |
| 42 | + :param cloud_config: |
| 43 | + instance of the cloud specified by --os-cloud |
| 44 | + in the configuration file |
| 45 | + :return: None |
| 46 | + """ |
| 47 | + |
| 48 | + log_level = logging.WARNING |
| 49 | + log_file = cloud_config.config.get('log_file', None) |
| 50 | + if log_file: |
| 51 | + # setup the logging level |
| 52 | + get_log_level = cloud_config.config.get('log_level') |
| 53 | + if get_log_level: |
| 54 | + log_level = { |
| 55 | + 'error': logging.ERROR, |
| 56 | + 'info': logging.INFO, |
| 57 | + 'debug': logging.DEBUG, |
| 58 | + }.get(get_log_level, logging.WARNING) |
| 59 | + |
| 60 | + # setup the logging context |
| 61 | + log_cont = _LogContext( |
| 62 | + clouds_name=cloud_config.config.get('cloud'), |
| 63 | + project_name=cloud_config.auth.get('project_name'), |
| 64 | + username=cloud_config.auth.get('username'), |
| 65 | + ) |
| 66 | + # setup the logging handler |
| 67 | + log_handler = _setup_handler_for_logging( |
| 68 | + logging.FileHandler, |
| 69 | + log_level, |
| 70 | + file_name=log_file, |
| 71 | + context=log_cont, |
| 72 | + ) |
| 73 | + if log_level == logging.DEBUG: |
| 74 | + # DEBUG only. |
| 75 | + # setup the operation_log |
| 76 | + shell.enable_operation_logging = True |
| 77 | + shell.operation_log.setLevel(logging.DEBUG) |
| 78 | + shell.operation_log.addHandler(log_handler) |
| 79 | + |
| 80 | + |
| 81 | +def _setup_handler_for_logging(handler_type, level, file_name, context): |
| 82 | + """Setup of the handler |
| 83 | +
|
| 84 | + Setup of the handler for addition of the logging handler, |
| 85 | + changes of the logging format, change of the logging level, |
| 86 | +
|
| 87 | + :param handler_type: type of logging handler |
| 88 | + :param level: logging level |
| 89 | + :param file_name: name of log-file |
| 90 | + :param context: instance of _LogContext() |
| 91 | + :return: logging handler |
| 92 | + """ |
| 93 | + |
| 94 | + root_logger = logging.getLogger('') |
| 95 | + handler = None |
| 96 | + # Setup handler for FileHandler(--os-cloud) |
| 97 | + handler = logging.FileHandler( |
| 98 | + filename=file_name, |
| 99 | + ) |
| 100 | + formatter = _LogContextFormatter( |
| 101 | + context=context, |
| 102 | + fmt=_LOG_MESSAGE_FORMAT, |
| 103 | + datefmt=_LOG_DATE_FORMAT, |
| 104 | + ) |
| 105 | + handler.setFormatter(formatter) |
| 106 | + handler.setLevel(level) |
| 107 | + |
| 108 | + # If both `--log-file` and `--os-cloud` are specified, |
| 109 | + # the log is output to each file. |
| 110 | + root_logger.addHandler(handler) |
| 111 | + |
| 112 | + return handler |
| 113 | + |
| 114 | + |
| 115 | +class _LogContext(object): |
| 116 | + """Helper class to represent useful information about a logging context""" |
| 117 | + |
| 118 | + def __init__(self, clouds_name=None, project_name=None, username=None): |
| 119 | + """Initialize _LogContext instance |
| 120 | +
|
| 121 | + :param clouds_name: one of the cloud name in configuration file |
| 122 | + :param project_name: the project name in cloud(clouds_name) |
| 123 | + :param username: the user name in cloud(clouds_name) |
| 124 | + """ |
| 125 | + |
| 126 | + self.clouds_name = clouds_name |
| 127 | + self.project_name = project_name |
| 128 | + self.username = username |
| 129 | + |
| 130 | + def to_dict(self): |
| 131 | + return { |
| 132 | + 'clouds_name': self.clouds_name, |
| 133 | + 'project_name': self.project_name, |
| 134 | + 'username': self.username |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | +class _LogContextFormatter(logging.Formatter): |
| 139 | + """Customize the logging format for logging handler""" |
| 140 | + |
| 141 | + def __init__(self, *args, **kwargs): |
| 142 | + self.context = kwargs.pop('context', None) |
| 143 | + logging.Formatter.__init__(self, *args, **kwargs) |
| 144 | + |
| 145 | + def format(self, record): |
| 146 | + d = self.context.to_dict() |
| 147 | + for k, v in d.items(): |
| 148 | + setattr(record, k, v) |
| 149 | + return logging.Formatter.format(self, record) |
0 commit comments