Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need help in using regex with LOG_VIEWER_PATTERNS #29

Open
irfanhakim-as opened this issue Jan 14, 2022 · 2 comments
Open

Need help in using regex with LOG_VIEWER_PATTERNS #29

irfanhakim-as opened this issue Jan 14, 2022 · 2 comments

Comments

@irfanhakim-as
Copy link

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!

@irfanhakim-as irfanhakim-as changed the title Need help with Need help with LOG_VIEWER_PATTERNS Jan 14, 2022
@irfanhakim-as irfanhakim-as changed the title Need help with LOG_VIEWER_PATTERNS Need help in using regex with LOG_VIEWER_PATTERNS Jan 14, 2022
@agusmakmun
Copy link
Owner

agusmakmun commented Jan 16, 2022

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,

if next_char == '\n' and line:
if any([line.endswith(p) for p in reversed_patterns]):
if exclude in line[::-1]:

@irfanhakim-as
Copy link
Author

irfanhakim-as commented Jan 20, 2022

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,

if next_char == '\n' and line:
if any([line.endswith(p) for p in reversed_patterns]):
if exclude in line[::-1]:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants