Commit 8ef107c6 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Make pid logging optional, logs are now readable

Refs: #4242
parent ee1dbfc6
......@@ -113,12 +113,23 @@ class _command_init(object):
def _set_log_params(self):
try:
self.client.LOG_TOKEN, self.client.LOG_DATA = (
self['config'].get_global('log_token').lower() == 'on',
self.client.LOG_TOKEN = (
self['config'].get_global('log_token').lower() == 'on')
except Exception as e:
log.debug('Failed to read custom log_token setting:'
'%s\n default for log_token is off' % e)
try:
self.client.LOG_DATA = (
self['config'].get_global('log_data').lower() == 'on')
except Exception as e:
log.debug('Failed to read custom log settings:'
'%s\n defaults for token and data logging are off' % e)
log.debug('Failed to read custom log_data setting:'
'%s\n default for log_data is off' % e)
try:
self.client.LOG_PID = (
self['config'].get_global('log_pid').lower() == 'on')
except Exception as e:
log.debug('Failed to read custom log_pid setting:'
'%s\n default for log_pid is off' % e)
def _update_max_threads(self):
if getattr(self, 'client', None):
......
......@@ -75,6 +75,7 @@ DEFAULTS = {
'log_file': os.path.expanduser('~/.kamaki.log'),
'log_token': 'off',
'log_data': 'off',
'log_pid': 'off',
'max_threads': 7,
'history_file': HISTORY_PATH,
'user_cli': 'astakos',
......
......@@ -100,6 +100,7 @@ class Logged(object):
LOG_TOKEN = False
LOG_DATA = False
LOG_PID = False
class RequestManager(Logged):
......@@ -143,18 +144,20 @@ class RequestManager(Logged):
self.scheme, self.netloc = self._connection_info(url, path, params)
def dump_log(self):
sendlog.info('%s %s://%s%s\t[%s]' % (
self.method, self.scheme, self.netloc, self.path, self))
plog = '\t[%s]' if self.LOG_PID else ''
sendlog.info('- - - - - - -')
sendlog.info('%s %s://%s%s%s' % (
self.method, self.scheme, self.netloc, self.path, plog))
for key, val in self.headers.items():
if (not self.LOG_TOKEN) and key.lower() == 'x-auth-token':
continue
sendlog.info(' %s: %s\t[%s]' % (key, val, self))
sendlog.info(' %s: %s%s' % (key, val, plog))
if self.data:
sendlog.info('data size:%s\t[%s]' % (len(self.data), self))
sendlog.info('data size:%s%s' % (len(self.data), plog))
if self.LOG_DATA:
sendlog.info(self.data)
else:
sendlog.info('data size:0\t[%s]' % self)
sendlog.info('data size:0%s' % plog)
sendlog.info('')
def perform(self, conn):
......@@ -177,7 +180,8 @@ class RequestManager(Logged):
wait = 0.03 * random()
sleep(wait)
keep_trying -= wait
logmsg = 'Kamaki Timeout %s %s\t[%s]' % (self.method, self.path, self)
plog = '\t[%s]' if self.LOG_PID else ''
logmsg = 'Kamaki Timeout %s %s%s' % (self.method, self.path, plog)
recvlog.debug(logmsg)
raise ClientError('HTTPResponse takes too long - kamaki timeout')
......@@ -210,15 +214,19 @@ class ResponseManager(Logged):
**pool_kw) as connection:
self.request.LOG_TOKEN = self.LOG_TOKEN
self.request.LOG_DATA = self.LOG_DATA
self.request.LOG_PID = self.LOG_PID
r = self.request.perform(connection)
plog = ''
if self.LOG_PID:
recvlog.info('\n%s <-- %s <-- [req: %s]\n' % (
self, r, self.request))
plog = '\t[%s]' % self
self._request_performed = True
self._status_code, self._status = r.status, unquote(
r.reason)
recvlog.info(
'%d %s\t[p: %s]' % (
self.status_code, self.status, self))
'%d %s%s' % (
self.status_code, self.status, plog))
self._headers = dict()
for k, v in r.getheaders():
if (not self.LOG_TOKEN) and (
......@@ -226,13 +234,13 @@ class ResponseManager(Logged):
continue
v = unquote(v)
self._headers[k] = v
recvlog.info(' %s: %s\t[p: %s]' % (k, v, self))
recvlog.info(' %s: %s%s' % (k, v, plog))
self._content = r.read()
recvlog.info('data size: %s\t[p: %s]' % (
len(self._content) if self._content else 0,
self))
recvlog.info('data size: %s%s' % (
len(self._content) if self._content else 0, plog))
if self.LOG_DATA and self._content:
recvlog.info('%s\t[p: %s]' % (self._content, self))
recvlog.info('%s%s' % (self._content, plog))
sendlog.info('- - - - - - -')
break
except Exception as err:
if isinstance(err, HTTPException):
......@@ -246,8 +254,7 @@ class ResponseManager(Logged):
'\n'.join(['%s' % type(err)] + format_stack()))
raise ClientError(
'Failed while http-connecting to %s (%s)' % (
self.request.url,
err))
self.request.url, err))
@property
def status_code(self):
......@@ -317,12 +324,10 @@ class SilentEvent(Thread):
self._exception = e
class Client(object):
class Client(Logged):
MAX_THREADS = 7
DATE_FORMATS = ['%a %b %d %H:%M:%S %Y', ]
LOG_TOKEN = False
LOG_DATA = False
CONNECTION_RETRY_LIMIT = 0
def __init__(self, base_url, token):
......@@ -405,14 +410,16 @@ class Client(object):
if data:
headers.setdefault('Content-Length', '%s' % len(data))
sendlog.debug('\n\nCMT %s@%s\t[%s]', method, self.base_url, self)
plog = '\t[%s]' if self.LOG_PID else ''
sendlog.debug('\n\nCMT %s@%s%s', method, self.base_url, plog)
req = RequestManager(
method, self.base_url, path,
data=data, headers=headers, params=params)
# req.log()
r = ResponseManager(
req, connection_retry_limit=self.CONNECTION_RETRY_LIMIT)
r.LOG_TOKEN, r.LOG_DATA = self.LOG_TOKEN, self.LOG_DATA
r.LOG_TOKEN, r.LOG_DATA, r.LOG_PID = (
self.LOG_TOKEN, self.LOG_DATA, self.LOG_PID)
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