简体   繁体   English

通过tail格式化和漂亮的打印日志

[英]Format and pretty print log via tail

I have this log file that I check on a frequent basis and because of the format of it, it's quite easier to read when pretty printed.我有这个日志文件,我经常检查它,由于它的格式,打印出来后更容易阅读。 I'd like to do so in a tail.我想这样做。

Logs in the file like:登录文件,如:

2019-07-04T09:53:04-07:00   some.package.placeholder.stderr {"log": "The content", "foo": "bar", "baz": "blah"}
2019-07-04T10:15:37-07:00   some.package.placeholder.stderr {"log": "I'm actually", "foo": "bar", "baz": "blah"}
2019-07-04T10:15:37-07:00   some.package.placeholder.stderr {"log": "Interested on", "foo": "bar", "baz": "blah"}

And I want to do something similar to我想做类似的事情

tail -f myLogFile | grep [...?...] | jq '.log'

So when tailing I get:所以当尾随我得到:

The content
I'm actually
Interested on

Or even:甚至:

2019-07-04T09:53:04-07:00   The content
2019-07-04T10:15:37-07:00   I'm actually
2019-07-04T10:15:37-07:00   Interested on

With GNU grep for -o : 使用GNU grep -o

$ tail file | grep -o '{[^}]*}' | jq -r '.log'
The content
I'm actually
Interested on

With any awk: 任何awk:

$ tail file | awk 'sub(/.*{/,"{")' | jq -r '.log'
The content
I'm actually
Interested on

$ tail file | awk '{d=$1} sub(/.*{/,""){$0="{\"date\": \""d"\", " $0} 1' | jq -r '.date + " " + .log'
2019-07-04T09:53:04-07:00 The content
2019-07-04T10:15:37-07:00 I'm actually
2019-07-04T10:15:37-07:00 Interested on

That last one works by merging the date field from the input into the json so then jq can just select and print it with the log field. 最后一个通过将输入中的日期字段合并到json中来工作,然后jq可以选择它并将其与日志字段一起打印。

If the log lines are tab delimited, you can read the lines in raw and split on tabs. 如果日志行由制表符分隔,则可以读取原始行并在制表符上拆分行。 Which you could then parse the json and filter to your hearts content, and recombine as necessary. 然后您可以解析json并过滤到您的hearts内容,并在必要时进行重组。

$ tail -f myLogFile | jq -Rr 'split("\t") | [.[0], (.[2] | fromjson.log)] | join("\t")'
2019-07-04T09:53:04-07:00   The content
2019-07-04T10:15:37-07:00   I'm actually
2019-07-04T10:15:37-07:00   Interested on

Here's a thing I use that can be used in a pipe and with file args:这是我使用的一个东西,可以在管道和文件 args 中使用:

cat /usr/local/bin/j2t
#!/bin/bash

function usage {
  cat <<EOF
Usage:
        $0 <json filename>
    or
        tail -F <json filename> | $0
EOF
}

if (($# == 0)); then
    {
        sed "s/@\(timestamp\)/\1/" | jq -r '[.timestamp, .pri.facility, .pri.severity, .message] | join("\t")'
    } < /dev/stdin

else
    if [ -r "$1" ] ; then
        sed "s/@\(timestamp\)/\1/" $1 | jq -r '[.timestamp, .pri.facility, .pri.severity, .message] | join("\t")'
    else
        help
    fi
fi

eg: (if your daemon.log is json)例如:(如果您的 daemon.log 是 json)

j2t /var/log/daemon.log
level: 63, builder: awillia2)
2021-08-14T00:00:06.820642+00:00        daemon  INFO     Starting Run Clamscan...
2021-08-14T00:00:06.846405+00:00        daemon  INFO     Started Run Clamscan.

Should probably reformat the time, it's a bit long.可能应该重新格式化时间,它有点长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM