How do I run jq on each log line and tail it?

Viewed 1414

I have a log file with each line being a json object. I want to view the log lines as pretty json while being able to tail the logs, with maybe tail or less. I have tried the following and they either return immediately i.e. don't tail the logs, or appear to tail (by not returning) but don't update with new logs

less jsonlines.log | jq "." 
tail -f jsonlines.log | jq "."
1 Answers

"Works for me"™:

  • in one terminal window:

    while true; do echo "{\"date\":\"$(date)\"}" >> logfile; sleep 1; done
    
  • in another:

    tail -f logfile | jq .
    
Related