是否可以使用AWSlogs代理将我的应用程序的JSON日志输出发送到CloudWatch?
我有一个Python应用程序(Dockerized),该应用程序以JSON格式输出日志。 我想将这些日志发送到CloudWatch。为了避免进行任何代码更改,我希望使用AWSlogs CloudWatch代理将日志文件同步到CloudWatch,但是在查看CloudWatch Agent的时间戳格式“ TimestAmp_Format”之后,它希望我的应用程序以:
datetime_format = %d-%b-%Y %H:%M:%S UTC
但是我的应用程序将时间戳登录为JSON值,例如:
{"log":"{\"levelname\": \"INFO\", \"message\": \"example log\", \"filename\": \"application.py\", "time":"2022-05-16T11:36:03.04390948Z"}
我可以使用代理将这些日志发送到CloudWatch吗?如果没有,最好的选择是什么? 谢谢
I have a python application (dockerised) which output's logs in JSON format.
I'd like to send these logs to Cloudwatch. To avoid making any code changes i was hoping to use the awslogs Cloudwatch agent to sync the log file to Cloudwatch, but after looking at the Cloudwatch agent's timestamp format field 'timestamp_format', it's expecting my application to output the timestamp in a format like:
datetime_format = %d-%b-%Y %H:%M:%S UTC
But my application logs the timestamp as JSON value, e.g:
{"log":"{\"levelname\": \"INFO\", \"message\": \"example log\", \"filename\": \"application.py\", "time":"2022-05-16T11:36:03.04390948Z"}
Is it possible for me to send these logs to Cloudwatch using the agent? If not, what would be the best alternative?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
You can update the
timestamp_format
field to match your logs by updating the从您的JSON字段中,您的Timestamp格式在CloudWatch Agent的Config Agent's Config Agent的config config file(命名
config.json
默认情况下):如AWS文档中所述,
%f
for for分数秒的格式从一位到九位数字。我已经通过CloudWatch代理成功地发送了日志,此timestamp_format
用于使用三位数和九位数分数第二精度的JSON日志中的时间戳。请注意,如果您未指定
timezone
为utc
,则该值将默认为local
- 当心日光储蓄!You can update the
timestamp_format
field to match your logs by updating the configuration file for the CloudWatch Agent.From your JSON field, your timestamp format should look like this in your CloudWatch Agent's config file (named
config.json
by default):As mentioned in the AWS documentation, the
%f
formatter for fractional seconds matches from one to nine digits. I've successfully sent logs via the CloudWatch Agent with thistimestamp_format
for timestamps in JSON logs with three-digit and nine-digit fractional second precision.Note that if you don't specify the
timezone
asUTC
, then the value will default toLocal
- beware of daylight savings!