diff options
| author | Vishvananda Ishaya <vishvananda@gmail.com> | 2012-06-01 23:12:56 +0000 |
|---|---|---|
| committer | Vishvananda Ishaya <vishvananda@gmail.com> | 2012-06-07 21:25:57 +0000 |
| commit | 197a424bb43c57c6362d3df4c0d1c9528e823b5d (patch) | |
| tree | 2a7eca9009e9031ab8218b039e68baa5c0e36954 | |
| parent | 089300fd4bcecaacd2ea3fa345a1b6b2c5b2ab61 (diff) | |
Add user_name project_name and color option to log
* adds project_name and user_name to context for logging
* adds color argument to logging which allows for colored
log output based on the log level
Change-Id: If37d646fdba77d4214f72b19e5df02da5f7dbac6
| -rw-r--r-- | nova/api/auth.py | 4 | ||||
| -rw-r--r-- | nova/api/ec2/__init__.py | 4 | ||||
| -rw-r--r-- | nova/context.py | 9 | ||||
| -rw-r--r-- | nova/log.py | 17 |
4 files changed, 31 insertions, 3 deletions
diff --git a/nova/api/auth.py b/nova/api/auth.py index 7106bee7f..72eba1d9c 100644 --- a/nova/api/auth.py +++ b/nova/api/auth.py @@ -84,6 +84,8 @@ class NovaKeystoneContext(wsgi.Middleware): else: # This is for legacy compatibility project_id = req.headers['X_TENANT'] + project_name = req.headers.get('X_TENANT_NAME') + user_name = req.headers.get('X_USER_NAME') # Get the auth token auth_token = req.headers.get('X_AUTH_TOKEN', @@ -95,6 +97,8 @@ class NovaKeystoneContext(wsgi.Middleware): remote_address = req.headers.get('X-Forwarded-For', remote_address) ctx = context.RequestContext(user_id, project_id, + user_name=user_name, + project_name=project_name, roles=roles, auth_token=auth_token, remote_address=remote_address) diff --git a/nova/api/ec2/__init__.py b/nova/api/ec2/__init__.py index 8f0666d82..c9dbd36f8 100644 --- a/nova/api/ec2/__init__.py +++ b/nova/api/ec2/__init__.py @@ -247,6 +247,8 @@ class EC2KeystoneAuth(wsgi.Middleware): token_id = result['access']['token']['id'] user_id = result['access']['user']['id'] project_id = result['access']['token']['tenant']['id'] + user_name = result['access']['user'].get('name') + project_name = result['access']['token']['tenant'].get('name') roles = [role['name'] for role in result['access']['user']['roles']] except (AttributeError, KeyError), e: @@ -260,6 +262,8 @@ class EC2KeystoneAuth(wsgi.Middleware): remote_address) ctxt = context.RequestContext(user_id, project_id, + user_name=user_name, + project_name=project_name, roles=roles, auth_token=token_id, remote_address=remote_address) diff --git a/nova/context.py b/nova/context.py index bca5dec0f..2b97b0f54 100644 --- a/nova/context.py +++ b/nova/context.py @@ -43,7 +43,8 @@ class RequestContext(object): def __init__(self, user_id, project_id, is_admin=None, read_deleted="no", roles=None, remote_address=None, timestamp=None, request_id=None, auth_token=None, overwrite=True, - quota_class=None, **kwargs): + quota_class=None, user_name=None, project_name=None, + **kwargs): """ :param read_deleted: 'no' indicates deleted records are hidden, 'yes' indicates deleted records are visible, 'only' indicates that @@ -83,6 +84,8 @@ class RequestContext(object): # rs_limits turnstile pre-processor. # See https://lists.launchpad.net/openstack/msg12200.html self.quota_class = quota_class + self.user_name = user_name + self.project_name = project_name if overwrite or not hasattr(local.store, 'context'): self.update_store() @@ -115,7 +118,9 @@ class RequestContext(object): 'timestamp': utils.strtime(self.timestamp), 'request_id': self.request_id, 'auth_token': self.auth_token, - 'quota_class': self.quota_class} + 'quota_class': self.quota_class, + 'user_name': self.user_name, + 'project_name': self.project_name} @classmethod def from_dict(cls, values): diff --git a/nova/log.py b/nova/log.py index 310656bc5..19ad95b70 100644 --- a/nova/log.py +++ b/nova/log.py @@ -279,6 +279,21 @@ class LegacyNovaFormatter(logging.Formatter): return '\n'.join(formatted_lines) +class NovaColorHandler(logging.StreamHandler): + LEVEL_COLORS = { + logging.DEBUG: '\033[00;32m', # GREEN + logging.INFO: '\033[00;36m', # CYAN + logging.AUDIT: '\033[01;36m', # BOLD CYAN + logging.WARN: '\033[01;33m', # BOLD YELLOW + logging.ERROR: '\033[01;31m', # BOLD RED + logging.CRITICAL: '\033[01;31m', # BOLD RED + } + + def format(self, record): + record.color = self.LEVEL_COLORS[record.levelno] + return logging.StreamHandler.format(self, record) + + class PublishErrorsHandler(logging.Handler): def emit(self, record): if 'list_notifier_drivers' in FLAGS: @@ -357,7 +372,7 @@ def _setup_logging_from_flags(): os.chmod(logpath, mode) if FLAGS.use_stderr: - streamlog = logging.StreamHandler() + streamlog = NovaColorHandler() nova_root.addHandler(streamlog) elif not FLAGS.log_file: |
