如何阻止 journald 删除空白行?

如何阻止 journald 删除空白行?

我有一个应用程序以如下格式记录到标准输出:

incoming request from x.x.x.x
client version is 1.2
authenticated as [email protected]
processed 1234 bytes
closing connection

rejecting connection from y.y.y.y
client subnet is not on the list of allowed subnets

incoming request from z.z.z.z
client version is 1.6
authenticated as [email protected]
WARN: {{lang}} is not set for [email protected]
processed 2345 bytes
closing connection

你可以想象,空白行构成了这种格式很多更容易阅读。当我将其作为 systemd 服务运行并使用 查看输出时journalctl -fu,似乎换行符被删除了。我该如何防止这种情况发生?

答案1

我不确定您使用的是哪个版本或是否应用了任何过滤器,但对于 journalctl 默认输出是short

short :是默认值,生成与经典 syslog 文件格式大致相同的输出,每个日记条目显示一行。

如果您想要以不同的格式输出,则需要-o根据需要使用选项并提供字段。希望这会对您有所帮助。来自以下部分的手动粘贴。

       -o, --output=
       Controls the formatting of the journal entries that are shown. Takes one of the following options:

       short
           is the default and generates an output that is mostly identical to the formatting of classic syslog files, showing one line
           per journal entry.

       short-iso
           is very similar, but shows ISO 8601 wallclock timestamps.

       short-precise
           is very similar, but shows timestamps with full microsecond precision.

       short-monotonic
           is very similar, but shows monotonic timestamps instead of wallclock timestamps.

       verbose
           shows the full-structured entry items with all fields.

       export
           serializes the journal into a binary (but mostly text-based) stream suitable for backups and network transfer (see Journal
           Export Format[1] for more information).

       json
           formats entries as JSON data structures, one per line (see Journal JSON Format[2] for more information).

       json-pretty
           formats entries as JSON data structures, but formats them in multiple lines in order to make them more readable by humans.

       json-sse
           formats entries as JSON data structures, but wraps them in a format suitable for Server-Sent Events[3].

       cat
           generates a very terse output, only showing the actual message of each journal entry with no metadata, not even a timestamp.

相关内容