Return x-last lines from logs. closes #155

This commit is contained in:
Ruud
2012-04-30 18:51:37 +02:00
parent a19952e06d
commit ffd237e31b
2 changed files with 52 additions and 1 deletions
+1 -1
View File
@@ -59,7 +59,7 @@ def getParam(attr, default = None):
try:
return toUnicode(unquote_plus(getattr(flask.request, 'args').get(attr, default))).encode('utf-8')
except:
return None
return default
def padded_jsonify(callback, *args, **kwargs):
content = str(callback) + '(' + json.dumps(dict(*args, **kwargs)) + ')'
+51
View File
@@ -21,6 +21,17 @@ class Logging(Plugin):
'success': True,
'log': string, //Log file
'total': int, //Total log files available
}"""}
})
addApiView('logging.partial', self.partial, docs = {
'desc': 'Get a partial log',
'params': {
'type': {'desc': 'Type of log', 'type': 'string: all(default), error, info, debug'},
'lines': {'desc': 'Number of lines. Last to first. Default 30'},
},
'return': {'type': 'object', 'example': """{
'success': True,
'log': string, //Log file
}"""}
})
addApiView('logging.clear', self.clear, docs = {
@@ -64,6 +75,46 @@ class Logging(Plugin):
'total': total,
})
def partial(self):
log_type = getParam('type', 'all')
total_lines = getParam('lines', 30)
log_lines = []
for x in range(0, 50):
path = '%s%s' % (Env.get('log_path'), '.%s' % x if x > 0 else '')
# Check see if the log exists
if not os.path.isfile(path):
break
reversed_lines = []
f = open(path, 'r')
reversed_lines = f.read().split('[0m\n')
reversed_lines.reverse()
brk = False
for line in reversed_lines:
#print '%s ' % log_type in line.lower()
if log_type == 'all' or '%s ' % log_type.upper() in line:
log_lines.append(line)
if len(log_lines) >= total_lines:
brk = True
break
if brk:
break
log_lines.reverse()
return jsonified({
'success': True,
'log': '[0m\n'.join(log_lines),
})
def clear(self):
for x in range(0, 50):