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