可以在不更改 conf 文件的情况下更改 RSYSLOG_FileFormat 时间戳输出吗?

问题描述

我目前有一个在 rsyslog.conf 中启用了 RSYSLOG_FileFormat 的系统。我不允许更改系统格式,因此我试图找到一种解决方法,使我能够将日志文件(在本例中为 /var/log/messages)的输出查看到所需的时间戳格式以下。原因是我可以更轻松地快速导航不需要那么高的精度的日志文件。非常感谢您的建议!

示例当前输出时间戳:

2020-12-17T19:05:34.118891+00:00

所需的输出

Dec 12 2020 19:05:34 

解决方法

使用 awk 和两个数组:

awk 'BEGIN{m[10]="Oct"; m[11]="Nov"; m[12]="Dec"}
     {
       # split first field from current row ("$1")
       # (here: "2020-12-17T19:05:34.118891+00:00") with
       # field separator "T",".",and "-" in five parts
       # to array "array"
       split($1,array,"[T.-]")

       # rebuild first field from current row with elements of array "array"
       $1=sprintf("%s %s %s %s %s",m[array[2]],array[3],array[1],array[4],$2)

       # output complete current row
       print
     }' /var/log/messages

作为一行:

awk 'BEGIN{m[10]="Oct"; m[11]="Nov"; m[12]="Dec"} {split($1,"[T.-]"); $1=sprintf("%s %s %s %s %s",$2); print}' /var/log/messages

请完成自己的数组m