agusmakmun / django-log-viewer

Django Log Viewer allows you to read & download log files in the admin page
https://pypi.org/project/django-log-viewer/
MIT License
74 stars 30 forks source link

Need help in using regex with LOG_VIEWER_PATTERNS #29

Open irfanhakim-as opened 2 years ago

irfanhakim-as commented 2 years ago

My LOG_VIEWER_PATTERNS setting

LOG_VIEWER_PATTERNS = ['\d\d\d\d\d\d\d\dT\d\d\d\d\d\dZ#USR=\d#AU=\d#IU=\d#DVC=\d#AD=\d#PD=\d']

How my log looks like on django-log-viewer with this setting (pattern not working as I would hope):

20220113T142500Z#USR=1#AU=1#IU=0#DVC=0#AD=0#PD=020220113T213000Z#USR=1#AU=1#IU=0#DVC=0#AD=0#PD=1 

How the log is formatted:

'fticks':{
            'format':'{asctime}{message}',
            'datefmt':'%Y%m%dT%H%M%SZ',
            'style':'{',
        },
log_message='#USR={}#AU={}#IU={}#DVC={}#AD={}#PD={}'.format(ldap_users,active_users,inactive_users,devices,approved_devices,pending_devices)

An external test of the same regex pattern I've used with django-log-viewer successfully parses this:

# regex-test.py
import re

stringLog='20220113T142500Z#USR=1#AU=1#IU=0#DVC=0#AD=0#PD=020220113T213000Z#USR=1#AU=1#IU=0#DVC=0#AD=0#PD=1'

pattern=re.compile(r'\d\d\d\d\d\d\d\dT\d\d\d\d\d\dZ#USR=\d#AU=\d#IU=\d#DVC=\d#AD=\d#PD=\d')
matches=pattern.finditer(stringLog)

for match in matches:
  print(match)
$ python regex-test.py
<re.Match object; span=(0, 48), match='20220113T142500Z#USR=1#AU=1#IU=0#DVC=0#AD=0#PD=0'>
<re.Match object; span=(48, 96), match='20220113T213000Z#USR=1#AU=1#IU=0#DVC=0#AD=0#PD=1'>

I know I have done something wrong here and would appreciate someone pointing out how I can get this log of mine to split into newlines instead of getting tangled into a single entry in the log viewer. Thanks!

agusmakmun commented 2 years ago

Hmm I see the problem, it because of this:

>>> LOG_VIEWER_PATTERNS = ['\d\d\d\d\d\d\d\dT\d\d\d\d\d\dZ#USR=\d#AU=\d#IU=\d#DVC=\d#AD=\d#PD=\d']
>>> reversed_patterns = [x[::-1] for x in LOG_VIEWER_PATTERNS]
>>> reversed_patterns
['d\\=DP#d\\=DA#d\\=CVD#d\\=UI#d\\=UA#d\\=RSU#Zd\\d\\d\\d\\d\\d\\Td\\d\\d\\d\\d\\d\\d\\d\\']
>>>

your \d is parsed to \\d after reversed.

meanwhile we didn't support yet for complex regex patterns,

https://github.com/agusmakmun/django-log-viewer/blob/607b5e48d9dd75d593f1087263aa978940b6cd6b/log_viewer/utils.py#L55-L57

irfanhakim-as commented 2 years ago

Hmm I see the problem, it because of this:

>>> LOG_VIEWER_PATTERNS = ['\d\d\d\d\d\d\d\dT\d\d\d\d\d\dZ#USR=\d#AU=\d#IU=\d#DVC=\d#AD=\d#PD=\d']
>>> reversed_patterns = [x[::-1] for x in LOG_VIEWER_PATTERNS]
>>> reversed_patterns
['d\\=DP#d\\=DA#d\\=CVD#d\\=UI#d\\=UA#d\\=RSU#Zd\\d\\d\\d\\d\\d\\Td\\d\\d\\d\\d\\d\\d\\d\\']
>>>

your \d is parsed to \\d after reversed.

meanwhile we didn't support yet for complex regex patterns,

https://github.com/agusmakmun/django-log-viewer/blob/607b5e48d9dd75d593f1087263aa978940b6cd6b/log_viewer/utils.py#L55-L57

Thanks for coming back. Is a workaround or solution available on how I may achieve to parse that log pattern of mine?

If there isn't, I'd like to exclude that log file entirely and only display other log files of mine that are much easier to parse (with levelname for example). But I'm having an issue with that as well as per #28