Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format and pretty print log via tail

I have this log file that I check on a frequent basis and because of the format of it, it's quite easier to read when pretty printed. I'd like to do so in a tail.

Logs in the file like:

2019-07-04T09:53:04-07:00   some.package.placeholder.stderr {"log": "The content", "foo": "bar", "baz": "blah"}
2019-07-04T10:15:37-07:00   some.package.placeholder.stderr {"log": "I'm actually", "foo": "bar", "baz": "blah"}
2019-07-04T10:15:37-07:00   some.package.placeholder.stderr {"log": "Interested on", "foo": "bar", "baz": "blah"}

And I want to do something similar to

tail -f myLogFile | grep [...?...] | jq '.log'

So when tailing I get:

The content
I'm actually
Interested on

Or even:

2019-07-04T09:53:04-07:00   The content
2019-07-04T10:15:37-07:00   I'm actually
2019-07-04T10:15:37-07:00   Interested on
like image 260
unmultimedio Avatar asked Mar 04 '23 18:03

unmultimedio


1 Answers

With GNU grep for -o:

$ tail file | grep -o '{[^}]*}' | jq -r '.log'
The content
I'm actually
Interested on

With any awk:

$ tail file | awk 'sub(/.*{/,"{")' | jq -r '.log'
The content
I'm actually
Interested on

$ tail file | awk '{d=$1} sub(/.*{/,""){$0="{\"date\": \""d"\", " $0} 1' | jq -r '.date + " " + .log'
2019-07-04T09:53:04-07:00 The content
2019-07-04T10:15:37-07:00 I'm actually
2019-07-04T10:15:37-07:00 Interested on

That last one works by merging the date field from the input into the json so then jq can just select and print it with the log field.

like image 90
Ed Morton Avatar answered Mar 16 '23 23:03

Ed Morton