Combine tail -F and json

My log files have one json object for each line. I use [json] [1] to get user-accessible output via

cat mylog.log | json -a field1 field2 

Now i would like to have

 tail -F mylog.log | json -a field1 field2 

for continuous output. But this does not seem to work, the shell just hangs. If I use &| for avoid buffering problems, the output looks as if I only run cat .

mylog.log as follows:

 {"field1": entry1a, "field2": entry2a, "field3": entry3a} {"field1": entry1b, "field2": entry2b, "field3": entry3b} 

Any suggestions?

[1] https://github.com/trentm/json

+4
source share
1 answer

It looks like json first loads all the stdin into the buffer and only then processes the data , but you should still be able to achieve stream processing by calling it for each of the lines added to the log file, something like this:

 tail -F mylog.log | while read line; do echo "$line" | json -a field1 field2; done 
+10
source

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


All Articles