How to capture the last part of a log line and interpret it as json?

I am looking at a log message in the following format

datetime log_message_type message_type server {json_string}

Thus, each line is separated by a space, always has the same fields for each line, and at the end it has a json line with different fields inside the json block.

I thought about it with a simple

with open('test.log', 'r') as f:
    for x in f:
        line = x.split()

        datetime         = line[0]
        log_message_type = line[1]
        message_type     = line[2]
        server           = line[3]
        json_string      = line[4]

This would work, except for spaces in my json string, for example, something like this.

{ "foo" : "bar" }

So by doing this, we would split my json string into spaces. Is there a way that I could use a regular expression or split something into spaces only until I get to the json string of the string and then save the rest? I tried to do something like

line = re.compile(".*\s.*\s.*\s.*\s").split(x)

4 json, , , regex python. - ?

: , python 2.7 .

+4
4

- . - .

log_line = "datetime log_message_type message_type server {json_string}"
json_part = log_line.split(None, 4)[-1]
+5

:

line = x.split(maxsplit=4)

>>> "a b c d my json expression".split(maxsplit=4)
['a', 'b', 'c', 'd', 'my json expression']

: python 2 , positional ( python 3 BTW):

line = x.split(None,4)
+6

python 3, .

long_string = "example example test test test test test test"
x1, x2, *tests = long_string.split()
tests = ' '.join(tests)
print(tests)
#test test test test test test
+2

- ?

line = "datetime log_message_type message_type server {json_string}" 

re.search(r"(\S+) (\S+) (\S+) (\S+) {(\S+)}", line).groups()

:

('datetime', 'log_message_type', 'message_type', 'server', 'json_string')
+1

Source: https://habr.com/ru/post/1667161/


All Articles