2

I use the pager called less since 20 years.

Time changes and I often look at files containing json.

A json dict which is on one line is not easy to read for me.

Is there a way to break the json into key-value pairs if you look at the log file?

Example:

How to display a line in a log file which looks like this:

{"timestamp": "2019-05-13 14:40:51", "name": "foo.views.error", "log_intent": "line1\nline2" ...}

roughly like this:

"timestamp": "2019-05-13 14:40:51"
"name": "foo.views.error"
"log_intent": "line1
line2"
....

I am not married with the pager less if there is better tool, please leave a comment.

xhienne
  • 5,738
  • 1
  • 15
  • 34
guettli
  • 25,042
  • 81
  • 346
  • 663
  • The thing is, the last 20 years you looked at logs from authors who understood that system administrators look at logfiles with less, or parse them with line-based tools like grep or sed. Now you look at logs from authors who don't use `less`, `grep` or `sed`. – hek2mgl May 14 '19 at 09:26
  • Formatting the json is definitely possible, and something I've been doing (had to do), but the solution very depends on the format of your logfile. – hek2mgl May 14 '19 at 09:28
  • Ok, I admit that your case seems simpler because every line is a valid json document and not plain text mixed with json. Overlooked that. But I keep thinking that plain text is the best format for logs. – hek2mgl May 14 '19 at 09:33

1 Answers1

3

In your case, the log file seems to consist of one json document per line, you can use jq to preformat the logfile before piping to less:

jq -s . file.log | less

With colors:

jq -Cs . file.log | less -r 
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 1
    This tool is new to me, but it sounds good: "jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text." https://stedolan.github.io/jq/ – guettli May 14 '19 at 10:17