python logging in AWS Fargate, datetime duplicated

huangapple go评论71阅读模式
英文:

python logging in AWS Fargate, datetime duplicated

问题

抱歉,我无法执行代码。以下是您要翻译的内容:

我正在尝试在AWS Fargate中使用Python日志模块。同一个应用程序也应该在本地运行,因此我想为本地使用创建一个自定义记录器,但保持CloudWatch日志不变。
这是我的做法:

    if logging.getLogger().hasHandlers():
        log = logging.getLogger()
        log.setLevel(logging.INFO)
    else:
        from logging.handlers import RotatingFileHandler
        log = logging.getLogger('sm')
        log.root.setLevel(logging.INFO)
        ...

但在CloudWatch中我得到了这个:

    2023-02-08T13:06:27.317+01:00	08/02/2023 12:06 - sm - INFO - Starting

在本地得到了这个:

    08/02/2023 12:06 - sm - INFO - Starting

我以为Fargate已经定义了一个记录器,但显然以下操作没有效果:

    logging.getLogger().hasHandlers()

理想情况下,CloudWatch中应该是期望的日志格式:

    2023-02-08T13:06:27.317+01:00   sm - INFO - Starting
英文:

I'm trying to use python logging module in AWS Fargate. The same application should work also locally, so I'd like to use a custom logger for local use but to keep intact cloudwatch logs.
This is what I'm doing:

if logging.getLogger().hasHandlers():
    log = logging.getLogger()
    log.setLevel(logging.INFO)
else:
    from logging.handlers import RotatingFileHandler
    log = logging.getLogger('sm')
    log.root.setLevel(logging.INFO)
    ...

But I get this in cloudwatch:

2023-02-08T13:06:27.317+01:00	08/02/2023 12:06 - sm - INFO - Starting

And this locally:

08/02/2023 12:06 - sm - INFO - Starting

I thought Fargate was already defining a logger, but apparently the following has no effect:

logging.getLogger().hasHandlers()

Ideally this should be the desired log in cloudwatch:

2023-02-08T13:06:27.317+01:00   sm - INFO - Starting

答案1

得分: 0

你可以使用Python的logging.basicConfig来配置根记录器。如果没有定义处理程序,debuginfowarningerrorcritical会自动调用basicConfig

logging.basicConfig(filename='test.log', format='%(filename)s: %(message)s', level=logging.DEBUG)

将日志格式设置为包括需要的参数详细信息:

logging.basicConfig(format='%(asctime)s   %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

使用这种方式来格式化CloudWatch中的日志。在这里找到一个Stack Overflow答案,其中有一些详细的解释。

英文:

You can use python logging basicconfig to configure the root logger. debug, info, warning, error and critical call basicConfig automatically if no handlers are defined.

logging.basicConfig(filename='test.log', format='%(filename)s: %(message)s',
level=logging.DEBUG)

set the logging format to include details which are required as args

logging.basicConfig(format='%(asctime)s   %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

use this to format logs in cloudwatch. Found one stackoverflow answer with some detailed explanation here

答案2

得分: 0

Fargate只是运行 Docker 容器。它不会为您运行在 Docker 容器中的 Python 代码进行任何设置。它甚至不知道也不关心您是否在运行 Python 代码。

由 Docker 容器的主要进程写入到 STDOUT/STDERR 的任何内容都会发送到 CloudWatch 日志,所以如果您希望与 ECS CloudWatch 日志兼容,只需确保将日志以您想要的格式发送到控制台。

英文:

Fargate just runs docker containers. It doesn't do any setup of your Python code that happens to be running in that docker container for you. It doesn't even know or care that you are running Python code.

Anything written to STDOUT/STDERR by the primary process of the docker container gets sent to CloudWatch Logs, so if you want to be compatible with ECS CloudWatch Logs just make sure you are sending logs in the format you want to the console.

huangapple
  • 本文由 发表于 2023年2月8日 21:33:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定