mat*_*ieu 9 systemd systemd-journald
systemd 似乎在多个日志消息中拆分了很长的日志行:
$ journalctl -u myunit
Nov 12 08:00:18 ovh7 uwsgi[32441]: SHORT LINE
Nov 12 08:00:18 ovh7 uwsgi[32441]: START of VERY VERY LONG LINE ON STDOUT
Nov 12 08:00:18 ovh7 uwsgi[32441]: CONTINUE VERY VERY LONG LINE
Nov 12 08:00:18 ovh7 uwsgi[32441]: SHORT LINE
Run Code Online (Sandbox Code Playgroud)
现在,我并不介意,但我需要加入单独的日志消息以从我的进程中取回原始标准输出。我以为我只能梳理 pid 的日志并使用 journalctl json 输出加入 MESSAGE 字符串:
def main():
import optparse, json, sys
parser = optparse.OptionParser()
parser.add_option('--pid')
parser.add_option('-f', '--file')
options, args = parser.parse_args()
with open(options.file, 'r') as f:
for line in f:
d = json.loads(line)
if d['_PID'] == options.pid:
sys.stdout.write(d['MESSAGE'].encode("utf-8"))
Run Code Online (Sandbox Code Playgroud)
可悲的是,上述方法不起作用,因为 systemd 似乎也从标准输出中修剪了尾随的 \n。它生成这个:
SHORT LINE START of VERY VERY LONG LINE ON STDOUT CONTINUE VERY VERY LONG LINE SHORT LINE
Run Code Online (Sandbox Code Playgroud)
现在,我可以尝试为每个 MESSAGE 添加一个额外的 \n ,但这会生成:
SHORT LINE
START of VERY VERY LONG LINE ON STDOUT
CONTINUE VERY VERY LONG LINE
SHORT LINE
Run Code Online (Sandbox Code Playgroud)
以上两个结果都不是很有帮助。我需要这个:
SHORT LINE
START of VERY VERY LONG LINE ON STDOUT CONTINUE VERY VERY LONG LINE
SHORT LINE
Run Code Online (Sandbox Code Playgroud)
And I see nothing in the output of journalctl that would allow me to infer that two consecutive messages are coming from the same line in the original stdout output. Any idea that would allow me to reconstruct this data correctly without having to generate a separate log file for my program's stdout ?
从 systemd v235 开始,systemd 有一个LineMax=指令 for journald.conf,默认设置为 48k。因此,如果您想在日志中支持很长的行,您的第一个选择是将其设置为更大的值。
json其次,如果您使用像( )这样的输出格式journalctl -o json,您可以查看是否插入了换行符。它会出现在 JSON 中,如下所示:
_LINE_BREAK: "line-max"
Run Code Online (Sandbox Code Playgroud)
如果您遇到这种情况,您就知道下一条记录是该行的延续。有关此功能的更多信息,您可以查看此 git commit:
https://github.com/systemd/systemd/commit/ec20fe5ffb8a00469bab209fff6c069bb93c6db2
| 归档时间: |
|
| 查看次数: |
552 次 |
| 最近记录: |