You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Curl为每个SSE事件打印日期时间戳?

给SSE流的每个事件添加时间戳

我执行以下curl命令请求SSE流:

curl --request POST --url http://some-example.com/stream   --header 'Content-Type: application/json'   --data '{
"text": "hello"
}'

Shell输出的每个SSE事件仅包含数据内容:

data:{"id":"chatcmpl-75BRIh9KeZv274g1AtHNq5d5POzEJ","object":"chat.completion.chunk","created":1681469968,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"role":"assistant"},"index":0,"finish_reason":null}]}

data:{"id":"chatcmpl-75BRIh9KeZv274g1AtHNq5d5POzEJ","object":"chat.completion.chunk","created":1681469968,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"hello"},"index":0,"finish_reason":null}]}

data:{"id":"chatcmpl-75BRIh9KeZv274g1AtHNq5d5POzEJ","object":"chat.completion.chunk","created":1681469968,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"."},"index":0,"finish_reason":null}]}

我希望每个SSE事件前都打印对应的日期时间,例如:

2023-04-14 08:13:12.418 data:{"id":"chatcmpl-75BRIh9KeZv274g1AtHNq5d5POzEJ","object":"chat.completion.chunk","created":1681469968,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"role":"assistant"},"index":0,"finish_reason":null}]}

2023-04-14 08:13:12.528 data:{"id":"chatcmpl-75BRIh9KeZv274g1AtHNq5d5POzEJ","object":"chat.completion.chunk","created":1681469968,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"hello"},"index":0,"finish_reason":null}]}

2023-04-14 08:13:12.568 data:{"id":"chatcmpl-75BRIh9KeZv274g1AtHNq5d5POzEJ","object":"chat.completion.chunk","created":1681469968,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"."},"index":0,"finish_reason":null}]}

试过用-v --trace-time参数,但没效果,请问怎么实现这个需求?


解决方法

可以通过管道结合文本处理工具,给curl输出的每一行(每个SSE事件)添加时间戳,以下是两种可行方案:

方案1:使用awk

将curl的输出传给awk,由awk在每行前拼接带毫秒的时间戳:

curl --request POST --url http://some-example.com/stream --header 'Content-Type: application/json' --data '{"text": "hello"}' | awk '{ print strftime("%Y-%m-%d %H:%M:%S.%3N"), $0 }'
  • 注:部分系统默认的awk(比如macOS的awk)不支持%3N毫秒格式,可通过brew install gawk安装GNU awk替代。

方案2:使用perl

perl的时间处理兼容性更强,适合多系统环境:
如果只需要秒级精度:

curl --request POST --url http://some-example.com/stream --header 'Content-Type: application/json' --data '{"text": "hello"}' | perl -ne 'use Time::HiRes qw(time); printf "%s %s", scalar localtime(time), $_'

需要毫秒级精度时:

curl --request POST --url http://some-example.com/stream --header 'Content-Type: application/json' --data '{"text": "hello"}' | perl -ne 'use Time::HiRes qw(gettimeofday); my ($sec, $usec) = gettimeofday(); my $msec = int($usec/1000); printf "%s.%03d %s", scalar localtime($sec), $msec, $_'

关于-v --trace-time无效的原因

--trace-time仅会给curl的调试日志(如请求头、响应头、连接信息等)添加时间戳,不会处理响应体中的SSE事件内容,因此对输出的data:行不起作用。


内容的提问来源于stack exchange,提问作者djy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 18:47:06