-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_log_parser.py
25 lines (19 loc) · 1.45 KB
/
5_log_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from pyparsing import Word, nums, oneOf, Suppress, restOfLine, alphas, alphanums, Group, ZeroOrMore, Optional, Keyword
# Parses Spring log lines (logs are taken from ENBD project)
log_sample = '''
2016-08-01 11:27:21.047 WARN 22458 --- [http-bio-8080-exec-24] c.m.enbd.core.AbstractEnbdController : POST /goal/daily: User haven't defined a goal yet
2016-08-01 17:54:47.908 INFO 22458 --- [http-bio-8080-exec-27] com.monitise.enbd.core.RequestFilter : Handling request, /version, POST
2016-08-01 17:54:47.909 INFO 22458 --- [http-bio-8080-exec-30] com.monitise.enbd.core.RequestFilter : Handling request, /features, POST
2016-08-01 17:54:47.914 DEBUG 22458 --- [http-bio-8080-exec-30] c.m.enbd.core.AbstractEnbdController : getting IOS features
2016-08-01 17:54:47.924 INFO 22458 --- [http-bio-8080-exec-27] c.m.enbd.core.AbstractEnbdController : checking version for device code: 1 and device OS: 10.0 and version: 1.0.3'''
date = Word(nums + '-')
time = Word(nums + ':.')
level = Keyword('INFO') | Keyword('WARN') | Keyword('DEBUG') # oneOf(['INFO', 'WARN', 'DEBUG'])
pid = Word(nums)
thread = Suppress('[') + Word(alphanums + '-') + Suppress(']')
source = Word(alphas + '.')
message = restOfLine
log_line = Group(date + time + level + pid + Suppress('---') + thread + source + Suppress(':') + message)
logs = ZeroOrMore(log_line)
print(logs.parseString(log_sample))
# So, if you design log strings that can be parsed, you can actually parse them