Commit 5fdccdec authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Allow config to switch some options on or off

- LOG_TOKEN: whether to log user token in http requests (default: off)
- LOG_DATA: whether to log http body (send or secv - default: off)

config options: log_token=off, log_data=off
parent 34b88989
......@@ -49,6 +49,12 @@ class _command_init(object):
except KeyError:
pass
def _update_low_level_log(self):
if hasattr(self, 'client'):
self.client.LOG_TOKEN, self.client.LOG_DATA = (
self['config'].get('global', 'log_token') == 'on',
self['config'].get('global', 'log_data') == 'on')
def _safe_progress_bar(self, msg, arg='progress_bar'):
"""Try to get a progress bar, but do not raise errors"""
try:
......
......@@ -51,9 +51,10 @@ class _astakos_init(_command_init):
base_url = self.config.get('astakos', 'url')\
or self.config.get('global', 'url')
self.client = AstakosClient(base_url=base_url, token=token)
self._update_low_level_log()
def main(self):
self._run
self._run()
@command(astakos_cmds)
......
......@@ -72,6 +72,7 @@ class _init_cyclades(_command_init):
base_url = self.config.get(service, 'url')\
or self.config.get('global', 'url')
self.client = CycladesClient(base_url=base_url, token=token)
self._update_low_level_log()
def main(self):
self._run()
......
......@@ -59,6 +59,7 @@ class _init_image(_command_init):
or self.config.get('compute', 'url')\
or self.config.get('global', 'url')
self.client = ImageClient(base_url=base_url, token=token)
self._update_low_level_log()
def main(self):
self._run()
......
......@@ -173,6 +173,7 @@ class _pithos_init(_command_init):
token=self.token,
account=self.account,
container=self.container)
self._update_low_level_log()
def main(self):
self._run()
......
......@@ -47,8 +47,6 @@ from kamaki.clients.utils import logger
DEBUG_LOG = logger.get_log_filename()
TIMEOUT = 60.0 # seconds
HTTP_METHODS = ['GET', 'POST', 'PUT', 'HEAD', 'DELETE', 'COPY', 'MOVE']
LOG_TOKEN = False
LOG_DATA = True
logger.add_file_logger('clients.send', __name__, filename=DEBUG_LOG)
sendlog = logger.get_logger('clients.send')
......@@ -111,7 +109,13 @@ class ClientError(Exception):
self.details = details if details else []
class RequestManager(object):
class Logged(object):
LOG_TOKEN = False
LOG_DATA = False
class RequestManager(Logged):
"""Handle http request information"""
def _connection_info(self, url, path, params={}):
......@@ -152,22 +156,22 @@ class RequestManager(object):
self.scheme, self.netloc = self._connection_info(url, path, params)
def log(self):
sendlog.debug('%s %s://%s%s\t[%s]' % (
sendlog.info('%s %s://%s%s\t[%s]' % (
self.method,
self.scheme,
self.netloc,
self.path,
self))
for key, val in self.headers.items():
if (not LOG_TOKEN) and key.lower() == 'x-auth-token':
if (not self.LOG_TOKEN) and key.lower() == 'x-auth-token':
continue
sendlog.debug('%s: %s\t[%s]', (key, val, self))
sendlog.info(' %s: %s\t[%s]' % (key, val, self))
if self.data:
sendlog.debug('data size:%s\t[%s]' % (len(self.data), self))
if LOG_DATA:
sendlog.info('data size:%s\t[%s]' % (len(self.data), self))
if self.LOG_DATA:
datasendlog.info(self.data)
else:
sendlog.debug('data size:0\t[%s]' % self)
sendlog.info('data size:0\t[%s]' % self)
sendlog.info('')
def perform(self, conn):
......@@ -195,7 +199,7 @@ class RequestManager(object):
raise ClientError('HTTPResponse takes too long - kamaki timeout')
class ResponseManager(object):
class ResponseManager(Logged):
"""Manage the http request and handle the response data, headers, etc."""
def __init__(self, request, poolsize=None):
......@@ -215,24 +219,26 @@ class ResponseManager(object):
with PooledHTTPConnection(
self.request.netloc, self.request.scheme,
**pool_kw) as connection:
self.request.LOG_TOKEN = self.LOG_TOKEN
self.request.LOG_DATA = self.LOG_DATA
r = self.request.perform(connection)
recvlog.debug('[resp: %s] <-- [req: %s]\n' % (r, self.request))
recvlog.info('[resp: %s] <-- [req: %s]\n' % (r, self.request))
self._request_performed = True
self._status_code, self._status = r.status, r.reason
recvlog.debug(
recvlog.info(
'%d %s\t[p: %s]' % (self.status_code, self.status, self))
self._headers = dict()
for k, v in r.getheaders():
if (not LOG_TOKEN) and k.lower() == 'x-auth-token':
if (not self.LOG_TOKEN) and k.lower() == 'x-auth-token':
continue
self._headers[k] = v
recvlog.debug(' %s: %s\t[p: %s]' % (k, v, self))
recvlog.info(' %s: %s\t[p: %s]' % (k, v, self))
self._content = r.read()
recvlog.debug('data size: %s\t[p: %s]' % (
recvlog.info('data size: %s\t[p: %s]' % (
len(self._content) if self._content else 0,
self))
if LOG_DATA and self._content:
datarecvlog.debug('%s\t[p: %s]' % (self._content, self))
if self.LOG_DATA and self._content:
datarecvlog.info('%s\t[p: %s]' % (self._content, self))
except Exception as err:
from traceback import format_stack
recvlog.debug('\n'.join(['%s' % type(err)] + format_stack()))
......@@ -316,6 +322,8 @@ class Client(object):
'%a %b %d %H:%M:%S %Y',
'%A, %d-%b-%y %H:%M:%S GMT',
'%a, %d %b %Y %H:%M:%S GMT']
LOG_TOKEN = False
LOG_DATA = False
def __init__(self, base_url, token):
self.base_url = base_url
......@@ -402,6 +410,7 @@ class Client(object):
data=data, headers=headers, params=params)
# req.log()
r = ResponseManager(req)
r.LOG_TOKEN, r.LOG_DATA = self.LOG_TOKEN, self.LOG_DATA
finally:
self.headers = dict()
self.params = dict()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment