Improve Plex log reader

This commit is contained in:
JonnyWong16 2021-11-29 21:25:48 -08:00
parent eee5402c80
commit d5d3766f90
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
3 changed files with 108 additions and 88 deletions

View file

@ -33,6 +33,19 @@
</select> </select>
</label> </label>
</div> </div>
% if plex_log_files:
<div class="btn-group" id="plex-log-files" style="display: none;">
<label>
<select name="plex-log-files-filter" id="plex-log-files-filter" class="btn" style="color: inherit;">
<option value="">Select Plex Media Server log file</option>
<option disabled>&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;</option>
% for file in plex_log_files:
<option value="${file}">${file}</option>
% endfor
</select>
</label>
</div>
% endif
<div class="btn-group" id="plex-log-levels" style="display: none;"> <div class="btn-group" id="plex-log-levels" style="display: none;">
<label> <label>
<select name="plex-log-level-filter" id="plex-log-level-filter" class="btn" style="color: inherit;"> <select name="plex-log-level-filter" id="plex-log-level-filter" class="btn" style="color: inherit;">
@ -47,7 +60,6 @@
</div> </div>
<button class="btn btn-dark" id="download-tautullilog"><i class="fa fa-download"></i> Download logs</button> <button class="btn btn-dark" id="download-tautullilog"><i class="fa fa-download"></i> Download logs</button>
<button class="btn btn-dark" id="download-plexserverlog" style="display: none;"><i class="fa fa-download"></i> Download logs</button> <button class="btn btn-dark" id="download-plexserverlog" style="display: none;"><i class="fa fa-download"></i> Download logs</button>
<button class="btn btn-dark" id="download-plexscannerlog" style="display: none;"><i class="fa fa-download"></i> Download logs</button>
<button class="btn btn-dark" id="clear-notify-logs" style="display: none;"><i class="fa fa-trash-o"></i> Clear logs</button> <button class="btn btn-dark" id="clear-notify-logs" style="display: none;"><i class="fa fa-trash-o"></i> Clear logs</button>
<button class="btn btn-dark" id="clear-newsletter-logs" style="display: none;"><i class="fa fa-trash-o"></i> Clear logs</button> <button class="btn btn-dark" id="clear-newsletter-logs" style="display: none;"><i class="fa fa-trash-o"></i> Clear logs</button>
<button class="btn btn-dark" id="clear-login-logs" style="display: none;"><i class="fa fa-trash-o"></i> Clear logs</button> <button class="btn btn-dark" id="clear-login-logs" style="display: none;"><i class="fa fa-trash-o"></i> Clear logs</button>
@ -59,7 +71,6 @@
<li role="presentation" class="active"><a id="tautulli-logs-btn" href="#tabs-tautulli_log" aria-controls="tabs-tautulli_log" role="tab" data-toggle="tab">Tautulli Logs</a></li> <li role="presentation" class="active"><a id="tautulli-logs-btn" href="#tabs-tautulli_log" aria-controls="tabs-tautulli_log" role="tab" data-toggle="tab">Tautulli Logs</a></li>
<li role="presentation"><a id="tautulli-api-logs-btn" href="#tabs-tautulli_api_log" aria-controls="tabs-tautulli_api_log" role="tab" data-toggle="tab">Tautulli API Logs</a></li> <li role="presentation"><a id="tautulli-api-logs-btn" href="#tabs-tautulli_api_log" aria-controls="tabs-tautulli_api_log" role="tab" data-toggle="tab">Tautulli API Logs</a></li>
<li role="presentation"><a id="plex-logs-btn" href="#tabs-plex_log" aria-controls="tabs-plex_log" role="tab" data-toggle="tab">Plex Media Server Logs</a></li> <li role="presentation"><a id="plex-logs-btn" href="#tabs-plex_log" aria-controls="tabs-plex_log" role="tab" data-toggle="tab">Plex Media Server Logs</a></li>
<li role="presentation"><a id="plex-scanner-logs-btn" href="#tabs-plex_scanner_log" aria-controls="tabs-plex_scanner_log" role="tab" data-toggle="tab">Plex Media Scanner Logs</a></li>
<li role="presentation"><a id="plex-websocket-logs-btn" href="#tabs-plex_websocket_log" aria-controls="tabs-plex_websocket_log" role="tab" data-toggle="tab">Plex Websocket Logs</a></li> <li role="presentation"><a id="plex-websocket-logs-btn" href="#tabs-plex_websocket_log" aria-controls="tabs-plex_websocket_log" role="tab" data-toggle="tab">Plex Websocket Logs</a></li>
<li role="presentation"><a id="notification-logs-btn" href="#tabs-notification_log" aria-controls="tabs-notification_log" role="tab" data-toggle="tab">Notification Logs</a></li> <li role="presentation"><a id="notification-logs-btn" href="#tabs-notification_log" aria-controls="tabs-notification_log" role="tab" data-toggle="tab">Notification Logs</a></li>
<li role="presentation"><a id="newsletter-logs-btn" href="#tabs-newsletter_log" aria-controls="tabs-newsletter_log" role="tab" data-toggle="tab">Newsletter Logs</a></li> <li role="presentation"><a id="newsletter-logs-btn" href="#tabs-newsletter_log" aria-controls="tabs-newsletter_log" role="tab" data-toggle="tab">Newsletter Logs</a></li>
@ -114,18 +125,6 @@
<tbody></tbody> <tbody></tbody>
</table> </table>
</div> </div>
<div role="tabpanel" class="tab-pane" id="tabs-plex_scanner_log">
<table class="display" id="plex_scanner_log_table" width="100%">
<thead>
<tr>
<th align="left" id="plex_scanner_timestamp">Timestamp</th>
<th align="left" id="plex_scanner_level">Level</th>
<th align="left" id="plex_scanner_message">Message</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<div role="tabpanel" class="tab-pane" id="tabs-notification_log"> <div role="tabpanel" class="tab-pane" id="tabs-notification_log">
<table class="display" id="notification_log_table" width="100%"> <table class="display" id="notification_log_table" width="100%">
<thead> <thead>
@ -268,24 +267,18 @@
}); });
} }
function loadPlexLogs() { function loadPlexLogs(logfile) {
plex_log_table_options.ajax = { plex_log_table_options.ajax = {
url: 'get_plex_log?log_type=server', url: 'get_plex_log',
type: 'POST' type: 'POST',
data: {
logfile: logfile
}
}; };
plex_log_table_options.initComplete = bindLogLevelFilter; plex_log_table_options.initComplete = bindLogLevelFilter;
plex_log_table = $('#plex_log_table').DataTable(plex_log_table_options); plex_log_table = $('#plex_log_table').DataTable(plex_log_table_options);
} }
function loadPlexScannerLogs() {
plex_log_table_options.ajax = {
url: 'get_plex_log?log_type=scanner',
type: 'POST'
};
plex_log_table_options.initComplete = bindLogLevelFilter;
plex_scanner_log_table = $('#plex_scanner_log_table').DataTable(plex_log_table_options);
}
function loadNotificationLogs() { function loadNotificationLogs() {
notification_log_table_options.ajax = { notification_log_table_options.ajax = {
url: 'get_notification_log', url: 'get_notification_log',
@ -327,11 +320,11 @@
$("#tautulli-logs-btn").click(function () { $("#tautulli-logs-btn").click(function () {
$("#tautulli-log-levels").show(); $("#tautulli-log-levels").show();
$("#plex-log-files").hide();
$("#plex-log-levels").hide(); $("#plex-log-levels").hide();
$("#clear-logs").show(); $("#clear-logs").show();
$("#download-tautullilog").show(); $("#download-tautullilog").show();
$("#download-plexserverlog").hide(); $("#download-plexserverlog").hide();
$("#download-plexscannerlog").hide();
$("#clear-notify-logs").hide(); $("#clear-notify-logs").hide();
$("#clear-newsletter-logs").hide(); $("#clear-newsletter-logs").hide();
$("#clear-login-logs").hide(); $("#clear-login-logs").hide();
@ -341,11 +334,11 @@
$("#tautulli-api-logs-btn").click(function () { $("#tautulli-api-logs-btn").click(function () {
$("#tautulli-log-levels").show(); $("#tautulli-log-levels").show();
$("#plex-log-files").hide();
$("#plex-log-levels").hide(); $("#plex-log-levels").hide();
$("#clear-logs").show(); $("#clear-logs").show();
$("#download-tautullilog").show(); $("#download-tautullilog").show();
$("#download-plexserverlog").hide(); $("#download-plexserverlog").hide();
$("#download-plexscannerlog").hide();
$("#clear-notify-logs").hide(); $("#clear-notify-logs").hide();
$("#clear-newsletter-logs").hide(); $("#clear-newsletter-logs").hide();
$("#clear-login-logs").hide(); $("#clear-login-logs").hide();
@ -355,11 +348,11 @@
$("#plex-websocket-logs-btn").click(function () { $("#plex-websocket-logs-btn").click(function () {
$("#tautulli-log-levels").show(); $("#tautulli-log-levels").show();
$("#plex-log-files").hide();
$("#plex-log-levels").hide(); $("#plex-log-levels").hide();
$("#clear-logs").show(); $("#clear-logs").show();
$("#download-tautullilog").show(); $("#download-tautullilog").show();
$("#download-plexserverlog").hide(); $("#download-plexserverlog").hide();
$("#download-plexscannerlog").hide();
$("#clear-notify-logs").hide(); $("#clear-notify-logs").hide();
$("#clear-newsletter-logs").hide(); $("#clear-newsletter-logs").hide();
$("#clear-login-logs").hide(); $("#clear-login-logs").hide();
@ -369,11 +362,11 @@
$("#plex-logs-btn").click(function () { $("#plex-logs-btn").click(function () {
$("#tautulli-log-levels").hide(); $("#tautulli-log-levels").hide();
$("#plex-log-files").show();
$("#plex-log-levels").show(); $("#plex-log-levels").show();
$("#clear-logs").hide(); $("#clear-logs").hide();
$("#download-tautullilog").hide(); $("#download-tautullilog").hide();
$("#download-plexserverlog").show(); $("#download-plexserverlog").show();
$("#download-plexscannerlog").hide();
$("#clear-notify-logs").hide(); $("#clear-notify-logs").hide();
$("#clear-newsletter-logs").hide(); $("#clear-newsletter-logs").hide();
$("#clear-login-logs").hide(); $("#clear-login-logs").hide();
@ -381,27 +374,13 @@
clearSearchButton('plex_log_table', plex_log_table); clearSearchButton('plex_log_table', plex_log_table);
}); });
$("#plex-scanner-logs-btn").click(function () {
$("#tautulli-log-levels").hide();
$("#plex-log-levels").show();
$("#clear-logs").hide();
$("#download-tautullilog").hide();
$("#download-plexserverlog").hide();
$("#download-plexscannerlog").show();
$("#clear-notify-logs").hide();
$("#clear-newsletter-logs").hide();
$("#clear-login-logs").hide();
loadPlexScannerLogs();
clearSearchButton('plex_scanner_log_table', plex_scanner_log_table);
});
$("#notification-logs-btn").click(function () { $("#notification-logs-btn").click(function () {
$("#tautulli-log-levels").hide(); $("#tautulli-log-levels").hide();
$("#plex-log-files").hide();
$("#plex-log-levels").hide(); $("#plex-log-levels").hide();
$("#clear-logs").hide(); $("#clear-logs").hide();
$("#download-tautullilog").hide(); $("#download-tautullilog").hide();
$("#download-plexserverlog").hide(); $("#download-plexserverlog").hide();
$("#download-plexscannerlog").hide();
$("#clear-notify-logs").show(); $("#clear-notify-logs").show();
$("#clear-newsletter-logs").hide(); $("#clear-newsletter-logs").hide();
$("#clear-login-logs").hide(); $("#clear-login-logs").hide();
@ -411,11 +390,11 @@
$("#newsletter-logs-btn").click(function () { $("#newsletter-logs-btn").click(function () {
$("#tautulli-log-levels").hide(); $("#tautulli-log-levels").hide();
$("#plex-log-files").hide();
$("#plex-log-levels").hide(); $("#plex-log-levels").hide();
$("#clear-logs").hide(); $("#clear-logs").hide();
$("#download-tautullilog").hide(); $("#download-tautullilog").hide();
$("#download-plexserverlog").hide(); $("#download-plexserverlog").hide();
$("#download-plexscannerlog").hide();
$("#clear-notify-logs").hide(); $("#clear-notify-logs").hide();
$("#clear-newsletter-logs").show(); $("#clear-newsletter-logs").show();
$("#clear-login-logs").hide(); $("#clear-login-logs").hide();
@ -425,11 +404,11 @@
$("#login-logs-btn").click(function () { $("#login-logs-btn").click(function () {
$("#tautulli-log-levels").hide(); $("#tautulli-log-levels").hide();
$("#plex-log-files").hide();
$("#plex-log-levels").hide(); $("#plex-log-levels").hide();
$("#clear-logs").hide(); $("#clear-logs").hide();
$("#download-tautullilog").hide(); $("#download-tautullilog").hide();
$("#download-plexserverlog").hide(); $("#download-plexserverlog").hide();
$("#download-plexscannerlog").hide();
$("#clear-notify-logs").hide(); $("#clear-notify-logs").hide();
$("#clear-newsletter-logs").hide(); $("#clear-newsletter-logs").hide();
$("#clear-login-logs").show(); $("#clear-login-logs").show();
@ -437,6 +416,12 @@
clearSearchButton('login_log_table', notification_log_table); clearSearchButton('login_log_table', notification_log_table);
}); });
$("#plex-log-files").on('change', function () {
var logfile = $("option:selected", this).val();
loadPlexLogs(logfile);
clearSearchButton('plex_log_table', plex_log_table);
});
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) { $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
$.fn.dataTable.tables({ visible: true, api: true }).columns.adjust(); $.fn.dataTable.tables({ visible: true, api: true }).columns.adjust();
}); });
@ -472,11 +457,8 @@
}); });
$("#download-plexserverlog").click(function () { $("#download-plexserverlog").click(function () {
window.location.href = "download_plex_log?log_type=server"; var logfile = $("option:selected", "#plex-log-files").val();
}); window.location.href = "download_plex_log?logfile=" + logfile;
$("#download-plexscannerlog").click(function () {
window.location.href = "download_plex_log?log_type=scanner";
}); });
$("#clear-notify-logs").click(function () { $("#clear-notify-logs").click(function () {
@ -559,8 +541,6 @@
log_table.ajax.reload(); log_table.ajax.reload();
} else if ($("#tabs-plex_log").hasClass("active")) { } else if ($("#tabs-plex_log").hasClass("active")) {
plex_log_table.ajax.reload(); plex_log_table.ajax.reload();
} else if ($("#tabs-plex_scanner_log").hasClass("active")) {
plex_scanner_log_table.ajax.reload();
} else if ($("#tabs-notificaiton_log").hasClass("active")) { } else if ($("#tabs-notificaiton_log").hasClass("active")) {
notification_log_table.ajax.reload(); notification_log_table.ajax.reload();
} else if ($("#tabs-login_log").hasClass("active")) { } else if ($("#tabs-login_log").hasClass("active")) {

View file

@ -29,19 +29,34 @@ else:
from plexpy import logger from plexpy import logger
def get_log_tail(window=20, parsed=True, log_type="server"): def list_plex_logs():
logs_dir = plexpy.CONFIG.PMS_LOGS_FOLDER
if plexpy.CONFIG.PMS_LOGS_FOLDER: if not logs_dir or logs_dir and not os.path.exists(logs_dir):
log_file = ""
if log_type == "server":
log_file = os.path.join(plexpy.CONFIG.PMS_LOGS_FOLDER, 'Plex Media Server.log')
elif log_type == "scanner":
log_file = os.path.join(plexpy.CONFIG.PMS_LOGS_FOLDER, 'Plex Media Scanner.log')
else:
return [] return []
log_files = []
for file in os.listdir(logs_dir):
if file.startswith('Plex Transcoder Statistics'):
# Plex Transcoder Statistics is an XML file
continue
if os.path.isfile(os.path.join(logs_dir, file)):
name, ext = os.path.splitext(file)
if ext == '.log' and not name[-1].isdigit():
log_files.append(name)
return log_files
def get_log_tail(window=20, parsed=True, log_file=''):
if not plexpy.CONFIG.PMS_LOGS_FOLDER:
return []
log_file = (log_file or 'Plex Media Server') + '.log'
log_file = os.path.join(plexpy.CONFIG.PMS_LOGS_FOLDER, log_file)
try: try:
logfile = open(log_file, "r", encoding="utf-8") logfile = open(log_file, 'r', encoding='utf-8')
except IOError as e: except IOError as e:
logger.error('Unable to open Plex Log file. %s' % e) logger.error('Unable to open Plex Log file. %s' % e)
return [] return []
@ -52,6 +67,8 @@ def get_log_tail(window=20, parsed=True, log_type="server"):
line_error = False line_error = False
clean_lines = [] clean_lines = []
for i in log_lines: for i in log_lines:
if not i.strip():
continue
try: try:
log_time = i.split(' [')[0] log_time = i.split(' [')[0]
log_level = i.split('] ', 1)[1].split(' - ', 1)[0] log_level = i.split('] ', 1)[1].split(' - ', 1)[0]
@ -74,7 +91,6 @@ def get_log_tail(window=20, parsed=True, log_type="server"):
return raw_lines return raw_lines
return log_lines
# http://stackoverflow.com/a/13790289/2405162 # http://stackoverflow.com/a/13790289/2405162
def tail(f, lines=1, _buffer=4098): def tail(f, lines=1, _buffer=4098):

View file

@ -2748,7 +2748,8 @@ class WebInterface(object):
@cherrypy.expose @cherrypy.expose
@requireAuth(member_of("admin")) @requireAuth(member_of("admin"))
def logs(self, **kwargs): def logs(self, **kwargs):
return serve_template(templatename="logs.html", title="Log") plex_log_files = log_reader.list_plex_logs()
return serve_template(templatename="logs.html", title="Log", plex_log_files=plex_log_files)
@cherrypy.expose @cherrypy.expose
@requireAuth(member_of("admin")) @requireAuth(member_of("admin"))
@ -2821,7 +2822,7 @@ class WebInterface(object):
@cherrypy.tools.json_out() @cherrypy.tools.json_out()
@requireAuth(member_of("admin")) @requireAuth(member_of("admin"))
@addtoapi() @addtoapi()
def get_plex_log(self, **kwargs): def get_plex_log(self, logfile='', **kwargs):
""" Get the PMS logs. """ Get the PMS logs.
``` ```
@ -2830,7 +2831,8 @@ class WebInterface(object):
Optional parameters: Optional parameters:
window (int): The number of tail lines to return window (int): The number of tail lines to return
log_type (str): "server" or "scanner" logfile (int): The name of the Plex log file,
e.g. "Plex Media Server", "Plex Media Scanner"
Returns: Returns:
json: json:
@ -2843,16 +2845,16 @@ class WebInterface(object):
] ]
``` ```
""" """
if kwargs.get('log_type'):
logfile = 'Plex Media ' + kwargs['log_type'].capitalize()
window = int(kwargs.get('window', plexpy.CONFIG.PMS_LOGS_LINE_CAP)) window = int(kwargs.get('window', plexpy.CONFIG.PMS_LOGS_LINE_CAP))
log_lines = []
log_type = kwargs.get('log_type', 'server')
try: try:
log_lines = {'data': log_reader.get_log_tail(window=window, parsed=True, log_type=log_type)} return {'data': log_reader.get_log_tail(window=window, parsed=True, log_file=logfile)}
except: except:
logger.warn("Unable to retrieve Plex Logs.") logger.warn("Unable to retrieve Plex log file '%'." % logfile)
return []
return log_lines
@cherrypy.expose @cherrypy.expose
@cherrypy.tools.json_out() @cherrypy.tools.json_out()
@ -4952,7 +4954,20 @@ class WebInterface(object):
@requireAuth(member_of("admin")) @requireAuth(member_of("admin"))
@addtoapi() @addtoapi()
def download_log(self, logfile='', **kwargs): def download_log(self, logfile='', **kwargs):
""" Download the Tautulli log file. """ """ Download the Tautulli log file.
```
Required parameters:
None
Optional parameters:
logfile (str): The name of the Tautulli log file,
"tautulli", "tautulli_api", "plex_websocket"
Returns:
download
```
"""
if logfile == "tautulli_api": if logfile == "tautulli_api":
filename = logger.FILENAME_API filename = logger.FILENAME_API
log = logger.logger_api log = logger.logger_api
@ -4973,26 +4988,35 @@ class WebInterface(object):
@cherrypy.expose @cherrypy.expose
@requireAuth(member_of("admin")) @requireAuth(member_of("admin"))
@addtoapi() @addtoapi()
def download_plex_log(self, **kwargs): def download_plex_log(self, logfile='', **kwargs):
""" Download the Plex log file. """ """ Download the Plex log file.
log_type = kwargs.get('log_type', 'server')
log_file = "" ```
if plexpy.CONFIG.PMS_LOGS_FOLDER: Required parameters:
if log_type == "server": None
log_file = 'Plex Media Server.log'
log_file_path = os.path.join(plexpy.CONFIG.PMS_LOGS_FOLDER, log_file) Optional parameters:
elif log_type == "scanner": logfile (int): The name of the Plex log file,
log_file = 'Plex Media Scanner.log' e.g. "Plex Media Server", "Plex Media Scanner"
log_file_path = os.path.join(plexpy.CONFIG.PMS_LOGS_FOLDER, log_file)
else: Returns:
download
```
"""
if not plexpy.CONFIG.PMS_LOGS_FOLDER:
return "Plex log folder not set in the settings." return "Plex log folder not set in the settings."
if kwargs.get('log_type'):
logfile = 'Plex Media ' + kwargs['log_type'].capitalize()
log_file = (logfile or 'Plex Media Server') + '.log'
log_file_path = os.path.join(plexpy.CONFIG.PMS_LOGS_FOLDER, log_file)
if log_file and os.path.isfile(log_file_path): if log_file and os.path.isfile(log_file_path):
return serve_download(log_file_path, name=log_file) log_file_name = os.path.basename(log_file_path)
return serve_download(log_file_path, name=log_file_name)
else: else:
return "Plex %s log file not found." % log_type return "Plex log file '%s' not found." % log_file
@cherrypy.expose @cherrypy.expose
@cherrypy.tools.json_out() @cherrypy.tools.json_out()