-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogs.nim
56 lines (55 loc) · 1.55 KB
/
logs.nim
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import binarylang, nimib, strutils, print, strformat
printColors = false
nbInit
nbText:"""
# Parsing logs with Binarylang
One thing that most developers will have to do at least
once in their life is be asked to parse some logs and extract
some sort of meaningful data from them. How do you do that with
binarylang? Here's an example from some web server logs.
"""
var log = readFile("example.log")
nbText: &"""```
{log}
```"""
nbText: """
That's a lot of stuff to parse. Thankfully, most of it appears to be
between strings, therefore it is pretty straightforward to parse it with
binarylang.
"""
nbCode:
struct(record):
s: ip
s: _ = " - - "
s: serverTime
s: _ = """ "GET /?time="""
s: clientTime
s: _ = "&random="
s: random
s: _ = "&webrtc="
s: webrtc
s: _ = "&websocket="
s: websocket
s: _ = """ HTTP/1.1" 200 """
s: _
s: _ = """ "https://588.lan.wtf/start/" """"
s: useragent
s: _ = "\"\n"
print log.toRecord
echo Record(
ip: "localhost", serverTime: "500", clientTime: "600", random: "23",
webrtc: "true", websocket: "true", useragent: "Chrome"
).fromRecord
nbText: """
And there you have it. We can not only parse logs, but now we can also generate
them if we wanted to. Lastly, we'll parse the entire thing into a sequence using binarylang.
"""
nbCode:
struct(logs):
*record: records{s.atEnd}
print log.toLogs.records[0..2]
nbText: """
We tell binarylang to keep parsing each log line until we hit the end
of the file (that is what the curly braces after the field indicate.)
"""
nbSave