mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-16 02:02:58 -07:00
Fix API get_logs (Fixes Tautulli/Tautulli-Issues#100)
This commit is contained in:
parent
a9b5c91f84
commit
2b395a7ad9
1 changed files with 36 additions and 34 deletions
|
@ -32,6 +32,7 @@ import xmltodict
|
||||||
import plexpy
|
import plexpy
|
||||||
import config
|
import config
|
||||||
import database
|
import database
|
||||||
|
import helpers
|
||||||
import libraries
|
import libraries
|
||||||
import logger
|
import logger
|
||||||
import mobile_app
|
import mobile_app
|
||||||
|
@ -173,47 +174,51 @@ class API2:
|
||||||
end = int(end)
|
end = int(end)
|
||||||
|
|
||||||
if regex:
|
if regex:
|
||||||
logger.api_debug(u'Tautulli APIv2 :: Filtering log using regex %s' % regex)
|
logger.api_debug(u"Tautulli APIv2 :: Filtering log using regex '%s'" % regex)
|
||||||
reg = re.compile('u' + regex, flags=re.I)
|
reg = re.compile(regex, flags=re.I)
|
||||||
|
|
||||||
for line in open(logfile, 'r').readlines():
|
with open(logfile, 'r') as f:
|
||||||
temp_loglevel_and_time = None
|
for line in f.readlines():
|
||||||
|
temp_loglevel_and_time = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
temp_loglevel_and_time = line.split('- ')
|
temp_loglevel_and_time = line.split('- ')
|
||||||
loglvl = temp_loglevel_and_time[1].split(' :')[0].strip()
|
loglvl = temp_loglevel_and_time[1].split(' :')[0].strip()
|
||||||
tl_tread = line.split(' :: ')
|
tl_tread = line.split(' :: ')
|
||||||
if loglvl is None:
|
if loglvl is None:
|
||||||
msg = line.replace('\n', '')
|
msg = line.replace('\n', '')
|
||||||
else:
|
else:
|
||||||
msg = line.split(' : ')[1].replace('\n', '')
|
msg = line.split(' : ')[1].replace('\n', '')
|
||||||
thread = tl_tread[1].split(' : ')[0]
|
thread = tl_tread[1].split(' : ')[0]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
# We assume this is a traceback
|
# We assume this is a traceback
|
||||||
tl = (len(templog) - 1)
|
tl = (len(templog) - 1)
|
||||||
templog[tl]['msg'] += line.replace('\n', '')
|
templog[tl]['msg'] += helpers.sanitize(unicode(line.replace('\n', ''), 'utf-8'))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if len(line) > 1 and temp_loglevel_and_time is not None and loglvl in line:
|
if len(line) > 1 and temp_loglevel_and_time is not None and loglvl in line:
|
||||||
|
|
||||||
d = {
|
d = {
|
||||||
'time': temp_loglevel_and_time[0],
|
'time': temp_loglevel_and_time[0],
|
||||||
'loglevel': loglvl,
|
'loglevel': loglvl,
|
||||||
'msg': msg.replace('\n', ''),
|
'msg': helpers.sanitize(unicode(msg.replace('\n', ''), 'utf-8')),
|
||||||
'thread': thread
|
'thread': thread
|
||||||
}
|
}
|
||||||
templog.append(d)
|
templog.append(d)
|
||||||
|
|
||||||
|
if order == 'desc':
|
||||||
|
templog = templog[::-1]
|
||||||
|
|
||||||
if end > 0 or start > 0:
|
if end > 0 or start > 0:
|
||||||
logger.api_debug(u'Tautulli APIv2 :: Slicing the log from %s to %s' % (start, end))
|
logger.api_debug(u"Tautulli APIv2 :: Slicing the log from %s to %s" % (start, end))
|
||||||
templog = templog[start:end]
|
templog = templog[start:end]
|
||||||
|
|
||||||
if sort:
|
if sort:
|
||||||
logger.api_debug(u'Tautulli APIv2 :: Sorting log based on %s' % sort)
|
logger.api_debug(u"Tautulli APIv2 :: Sorting log based on '%s'" % sort)
|
||||||
templog = sorted(templog, key=lambda k: k[sort])
|
templog = sorted(templog, key=lambda k: k[sort])
|
||||||
|
|
||||||
if search:
|
if search:
|
||||||
logger.api_debug(u'Tautulli APIv2 :: Searching log values for %s' % search)
|
logger.api_debug(u"Tautulli APIv2 :: Searching log values for '%s'" % search)
|
||||||
tt = [d for d in templog for k, v in d.items() if search.lower() in v.lower()]
|
tt = [d for d in templog for k, v in d.items() if search.lower() in v.lower()]
|
||||||
|
|
||||||
if len(tt):
|
if len(tt):
|
||||||
|
@ -222,16 +227,13 @@ class API2:
|
||||||
if regex:
|
if regex:
|
||||||
tt = []
|
tt = []
|
||||||
for l in templog:
|
for l in templog:
|
||||||
stringdict = ' '.join('{}{}'.format(k, v) for k, v in l.items())
|
stringdict = ' '.join(u'{}{}'.format(k, v) for k, v in l.items())
|
||||||
if reg.search(stringdict):
|
if reg.search(stringdict):
|
||||||
tt.append(l)
|
tt.append(l)
|
||||||
|
|
||||||
if len(tt):
|
if len(tt):
|
||||||
templog = tt
|
templog = tt
|
||||||
|
|
||||||
if order == 'desc':
|
|
||||||
templog = templog[::-1]
|
|
||||||
|
|
||||||
return templog
|
return templog
|
||||||
|
|
||||||
def get_settings(self, key=''):
|
def get_settings(self, key=''):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue