LoginSignup
4
3

More than 3 years have passed since last update.

JSON形式のtailfみたいなログストリームをjqで表示する

Last updated at Posted at 2019-12-26

ログファイルをtail -fdocker logs -fで見たりする事ありますよね。もし、そのログファイルがjsonフォーマットだったら各ログ毎にjqしたものを見たいですよね。

logs.sh
docker logs --tail 10 -f greatapp | while read line
do
  echo "$line" | jq -R -r '. as $line | try fromjson catch $line'
done

これがその答えです。

see also

How to skip non-json lines?
https://github.com/stedolan/jq/issues/884

𝑸: Is there a way to have jq keep going after it hits an error in the input file? Can jq handle broken JSON?
https://github.com/stedolan/jq/wiki/FAQ#general-questions

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3