是否可以使用AWSlogs代理将我的应用程序的JSON日志输出发送到CloudWatch?

发布于 2025-01-31 19:30:57 字数 502 浏览 3 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぽ尐不点ル 2025-02-07 19:30:57

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默认情况下):

{
    "logs": {
        "logs_collected": {
            "files": {
                "collect_list": [
                    {
                        "file_path": "/path/to/logfile.log",
                        "log_group_name": "log_group",
                        "log_stream_name": "log_stream",
                        "timezone": "UTC",
                        "timestamp_format": "%Y-%m-%dT%H:%M:%S.%f"
                    }
                ]
            }
        }
    }
}

如AWS文档中所述,%f for for分数秒的格式从一位到九位数字。我已经通过CloudWatch代理成功地发送了日志,此timestamp_format用于使用三位数和九位数分数第二精度的JSON日志中的时间戳。

请注意,如果您未指定timezoneutc,则该值将默认为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):

{
    "logs": {
        "logs_collected": {
            "files": {
                "collect_list": [
                    {
                        "file_path": "/path/to/logfile.log",
                        "log_group_name": "log_group",
                        "log_stream_name": "log_stream",
                        "timezone": "UTC",
                        "timestamp_format": "%Y-%m-%dT%H:%M:%S.%f"
                    }
                ]
            }
        }
    }
}

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 this timestamp_format for timestamps in JSON logs with three-digit and nine-digit fractional second precision.

Note that if you don't specify the timezone as UTC, then the value will default to Local - beware of daylight savings!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文