logs: filter logs by level (#42176)

This commit is contained in:
Lauréline Guérin 2020-04-28 11:44:38 +02:00
parent 14a4cce0ba
commit 9b8c7ce44b
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
4 changed files with 48 additions and 4 deletions

View File

@ -14,8 +14,9 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.utils.text import slugify
from django import forms
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _
class GenericConnectorForm(forms.ModelForm):
@ -23,3 +24,19 @@ class GenericConnectorForm(forms.ModelForm):
if not self.instance.slug:
self.instance.slug = slugify(self.instance.title)
return super(GenericConnectorForm, self).save(commit=commit)
class ResourceLogSearchForm(forms.Form):
log_level = forms.ChoiceField(
label=_('Log level'),
choices=(
('', _('All levels')),
('DEBUG', _('Debug')),
('INFO', _('Info')),
('WARNING', _('Warning')),
('ERROR', _('Error')),
('CRITICAL', _('Critical')),
),
required=False,
)
q = forms.CharField(required=False)

View File

@ -16,7 +16,8 @@
<div id="logs">
<form>
<p><input name="q" type="search" value="{{query}}"> <button>{% trans 'Search' %}</button>
<p>
{{ form.log_level }} <input name="q" type="search" value="{{ form.cleaned_data.q }}"> <button>{% trans 'Search' %}</button>
<span class="help_text">{% trans "(supports text search in messages, or dates)" %}</span>
</p>
</form>

View File

@ -18,6 +18,7 @@ import datetime
import hashlib
import inspect
import json
import logging
from django.apps import apps
from django.conf.urls import url
@ -54,6 +55,7 @@ from passerelle.utils.json import unflatten
from .utils import to_json, is_authorized
from .forms import GenericConnectorForm
from .forms import ResourceLogSearchForm
if 'mellon' in settings.INSTALLED_APPS:
from mellon.utils import get_idps
@ -228,7 +230,7 @@ class GenericViewLogsConnectorView(GenericConnectorMixin, ListView):
def get_context_data(self, **kwargs):
context = super(GenericViewLogsConnectorView, self).get_context_data(**kwargs)
context['object'] = self.get_object()
context['query'] = self.request.GET.get('q') or ''
context['form'] = self.form
if self.request.GET.get('log_id'):
try:
context['log_target'] = ResourceLog.objects.get(
@ -244,10 +246,17 @@ class GenericViewLogsConnectorView(GenericConnectorMixin, ListView):
return self.model.objects.get(slug=self.kwargs['slug'])
def get_queryset(self):
self.form = ResourceLogSearchForm(data=self.request.GET)
qs = ResourceLog.objects.filter(
appname=self.kwargs['connector'],
slug=self.kwargs['slug']).order_by('-timestamp')
query = self.request.GET.get('q')
query = None
level = None
if self.form.is_valid():
query = self.form.cleaned_data['q']
level = self.form.cleaned_data['log_level']
if level:
qs = qs.filter(levelno=logging.getLevelName(level))
if query:
try:
date = date_parser.parse(query, dayfirst=True)

View File

@ -208,6 +208,23 @@ def test_logs(app, admin_user):
resp = app.get('/manage/csvdatasource/test/logs/?log_id=foo')
assert 'log_target' not in resp.context
resp = app.get('/manage/csvdatasource/test/logs/?log_level=')
assert resp.text.count('<td class="timestamp">') == 4
assert resp.text.count('level-warning') == 2
assert resp.text.count('level-info') == 2
resp = app.get('/manage/csvdatasource/test/logs/?log_level=INFO')
assert resp.text.count('<td class="timestamp">') == 2
assert resp.text.count('level-warning') == 0
assert resp.text.count('level-info') == 2
resp = app.get('/manage/csvdatasource/test/logs/?log_level=ERROR')
assert resp.text.count('<td class="timestamp">') == 0
assert resp.text.count('level-warning') == 0
assert resp.text.count('level-info') == 0
resp = app.get('/manage/csvdatasource/test/logs/?log_level=foobar')
assert resp.text.count('<td class="timestamp">') == 4
assert resp.text.count('level-warning') == 2
assert resp.text.count('level-info') == 2
def test_logging_parameters(app, admin_user):
data = StringIO('1;Foo\n2;Bar\n3;Baz')