kevingilbert100
kevingilbert100

Reputation: 1333

Tailing a JSON log file with printf like output

Attempting to tail -f a log file with the following example contents.

{"name":"common","hostname":"kgilbert-mac.corp.realpage.com","pid":65184,"level":30,"msg":"iOT API listening at http://[::]:8080","time":"2017-01-11T00:20:26.359Z","v":0}
{"name":"common","hostname":"kgilbert-mac.corp.realpage.com","pid":65185,"level":30,"msg":"iOT API listening at http://[::]:8080","time":"2017-01-11T00:20:28.942Z","v":0}
{"name":"common","hostname":"kgilbert-mac.corp.realpage.com","pid":65187,"level":30,"msg":"iOT API listening at http://[::]:8080","time":"2017-01-11T00:20:30.221Z","v":0}

However I would like the output of the tail not to be the actual JSON string on each line, but a printf and limited version of it.

So maybe something like this

name: common | hostname: localhost | pid: 65187 | level: 30 | msg: iOT API listening at http://[::]:8080 | time: 2017-01-11T00:20:30.221Z

Upvotes: 2

Views: 1403

Answers (3)

JayCh
JayCh

Reputation: 36

Here's a python skeleton you can use as a utility (gotten by combining stack overflow answers):

#!/usr/bin/python

import time
import subprocess
import select
import json
from pprint import pprint

def processIt( string ):
    j = json.loads( string )
    for k in j:
        print "%s : %s" % ( k, j[k] )

f = subprocess.Popen(['tail','-F',"foo"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
p = select.poll()
p.register(f.stdout)

while True:
    if p.poll(1):
        try:
            processIt(f.stdout.readline())
        except:
            pass
    time.sleep(1)

Upvotes: 2

chepner
chepner

Reputation: 531235

This is relatively straightforward with jq:

tail -f | jq -r '"name: \(.name) | hostname: \(.hostname) | pid: \(.pid) | level: \(.level) | msg: \(.msg) | time: \(.time)"'

Upvotes: 2

innerpeace
innerpeace

Reputation: 112

try this :

tail json.txt | awk -F "," '{OFS="\t"; print $1,$2,$3,$4,$5,$6 }'

note that the json strint can contain "," in the key and value of its items. for safe, I recommand you to use python to convert json to formant like cvs, its more easy.

Upvotes: 3

Related Questions