jincept
jincept

Reputation: 279

Windows tail a log file with jq

I am using Windows 10 Powershell.

I want to tail a log file which is in JSON format and I also want to pick specific json fields.

Example logfile:

{"msg":"abc def1","time":"20:08","notneeded1":"xyz","notneeded2":"xyz"}
{"msg":"abc def2","time":"20:09","notneeded1":"xyz","notneeded2":"xyz"}
{"msg":"abc def3","time":"20:10","notneeded1":"xyz","notneeded2":"xyz"}

...

I can tail a log file live with Wait flag : Get-content -tail 10 -Wait 'C:\Desktop\data.log'

I can pick specific json fields via jq : Get-content -tail 10 'C:\Desktop\data.log'|jq '.time,.msg'

However I cannot use -Wait and jq together. Any suggestions on how to make it work?

Upvotes: 0

Views: 1424

Answers (1)

Doug Maurer
Doug Maurer

Reputation: 8868

You could make your own function to handle the powershell pipeline. Naturally you'll need to update the path to your jq file.

Function JQ {
    Param([parameter(valuefrompipeline)]$line)

    begin{
        $jq = "C:\temp\jq-win64.exe"
    }
    process{
        $line | &$Jq '.time,.msg'
    }
}

Get-Content $jsonfile -tail 10 -Wait | JQ

You could extend that by parameterizing the properties you want but this should get you started.

Upvotes: 0

Related Questions