Skip to content

Commit 424666a

Browse files
authored
Merge pull request #10 from kwangsooshin/feature/grap-log-file
Add: LOG_VIEWER_FILES_PATTERN
2 parents e3db50d + 20776bf commit 424666a

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Quick start
4444
::
4545

4646
LOG_VIEWER_FILES = ['logfile1', 'logfile2', ...]
47+
LOG_VIEWER_FILES_PATTERN = 'logfile*'
4748
LOG_VIEWER_FILES_DIR = os.path.join(BASE_DIR, '../logs')
4849
LOG_VIEWER_MAX_READ_LINES = 1000 # total log lines will be read
4950
LOG_VIEWER_PAGE_LENGTH = 25 # total log lines per-page

log_viewer/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.conf import settings
77

88
LOG_VIEWER_FILES = getattr(settings, 'LOG_VIEWER_FILES', [])
9+
LOG_VIEWER_FILES_PATTERN = getattr(settings, 'LOG_VIEWER_FILES_PATTERN', '*.log*')
910
LOG_VIEWER_FILES_DIR = getattr(settings, 'LOG_VIEWER_FILES_DIR', 'logs/')
1011
LOG_VIEWER_PAGE_LENGTH = getattr(settings, 'LOG_VIEWER_PAGE_LENGTH', 25)
1112
LOG_VIEWER_MAX_READ_LINES = getattr(settings, 'LOG_VIEWER_MAX_READ_LINES', 1000)

log_viewer/views.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import unicode_literals
33

44
import os
5+
from fnmatch import fnmatch
56
from itertools import islice
67
from django.http import HttpResponse
78
from django.views.generic import TemplateView
@@ -42,17 +43,16 @@ def get_log_json(self, original_context={}):
4243
len_logs_dir = len(settings.LOG_VIEWER_FILES_DIR)
4344

4445
for root, _, files in os.walk(settings.LOG_VIEWER_FILES_DIR):
45-
tmp_names = list(filter(lambda x: x.find('~') == -1, files))
46-
# if LOG_VIEWER_FILES is not set in settings
47-
# then all the files with '.log' extension are listed
48-
if len(settings.LOG_VIEWER_FILES) > 0:
49-
tmp_names = list(filter(lambda x: x in settings.LOG_VIEWER_FILES, tmp_names))
50-
else:
51-
tmp_names = [name for name in tmp_names if (name.split('.')[-1]) == 'log']
52-
53-
file_names += tmp_names
54-
file_display += [('%s/%s' % (root[len_logs_dir:], name))[1:] for name in tmp_names]
55-
file_urls += list(map(lambda x: '%s/%s' % (root, x), tmp_names))
46+
all_files = list(filter(lambda x: x.find('~') == -1, files))
47+
48+
log_files = []
49+
log_files.extend(list(filter(lambda x: x in settings.LOG_VIEWER_FILES, all_files)))
50+
log_files.extend([x for x in all_files if fnmatch(x, settings.LOG_VIEWER_FILES_PATTERN)])
51+
log_files = list(set(log_files))
52+
53+
file_names.extend(log_files)
54+
file_display.extend([('%s/%s' % (root[len_logs_dir:], name))[1:] for name in log_files])
55+
file_urls.extend(list(map(lambda x: '%s/%s' % (root, x), log_files)))
5656

5757
for i, element in enumerate(file_display):
5858
context['log_files'].append({

0 commit comments

Comments
 (0)