首页 > 解决方案 > 使用 create_export_task 将日志从 cloudwatch 导出到 s3 不会丢失一些日志

问题描述

我正在尝试将 cloudwatch 中过去 15 分钟的访问日志导出到 S3 存储桶,但是在运行它时,它成功地将日志存储到 S3 中,但是丢失了很多日志。例如,在过去 15 分钟的 cloudwatch 中,有 30 条日志,而在 S3 中它只有大约 3 条日志。

group_name = '/aws/elasticbeanstalk/my-env/var/app/current/storage/logs/laravel.log'

s3 = boto3.client('s3')
log_file = boto3.client('logs')

now = datetime.now()
deletionDate = now - timedelta(minutes=15)

start = math.floor(deletionDate.replace(second=0, microsecond=0).timestamp()*1000)
end = math.floor(now.replace(second=0, microsecond=0).timestamp()*1000)

destination_bucket = 'past15mins-log'
prefix = 'lambda2-test-log/'+str(start)+'-'+str(end)
# # TODO implement
response = log_file.create_export_task(
                logGroupName = group_name,
                fromTime = start, 
                to = end, 
                destination = destination_bucket,
                destinationPrefix = prefix
            )
if not response['ResponseMetadata']['HTTPStatusCode'] == 200:
    raise Exception('fail ' + str(start)+" - "+ str(end))

在文档中它说这是一个异步调用,我的猜测是因为有 3 个服务器可以从中获取日志,这会导致问题吗?

提前致谢。

标签: amazon-web-servicesamazon-s3

解决方案


推荐阅读