如何从AWS Fargate容器中运行的后台进程将日志发送到Cloudwatch?

问题描述 投票:0回答:2

我正在使用Fargate。我的容器正在运行两个进程。芹菜工作者在背景中和在前景的Django。前台进程将日志发送到stdout,因此AWS负责将Django日志发送到相关的Cloudwatch Log Group和Stream。

由于它在后台运行,如何将芹菜工作者的日志发送到(同一日志组中的不同日志流)Cloudwatch?

amazon-ecs amazon-cloudwatchlogs aws-fargate
2个回答
1
投票

如果无法将第二个进程移动到单独的容器并像往常一样进行记录,则可以将awslogs包安装到容器中并将其设置为读取后台进程的日志文件并将内容发送到CloudWatch。但我不推荐这种方法。


0
投票

同样,这不一定是基于Fargate的问题或问题。登录芹菜请查看http://docs.celeryproject.org/en/latest/userguide/tasks.html#logging

The worker won’t update the redirection if you create a logger instance somewhere in your task or task module.

If you want to redirect sys.stdout and sys.stderr to a custom logger you have to enable this manually, for example:

import sys

logger = get_task_logger(__name__)

@app.task(bind=True)
def add(self, x, y):
    old_outs = sys.stdout, sys.stderr
    rlevel = self.app.conf.worker_redirect_stdouts_level
    try:
        self.app.log.redirect_stdouts_to_logger(logger, rlevel)
        print('Adding {0} + {1}'.format(x, y))
        return x + y
    finally:
        sys.stdout, sys.stderr = old_outs

对于使用Fargate进行日志记录,我会使用awslogs驱动程序。以下是您在此处记录的配置方式:https://docs.aws.amazon.com/AmazonECS/latest/developerguide/using_awslogs.html

如果使用控制台:

enter image description here

或者如果在cloudformation模板中:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.