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
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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):
|
||||
|
||||
17
nova/log.py
17
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:
|
||||
|
||||
Reference in New Issue
Block a user