如何使用ddtrace将Python应用程序的日志发送到Datadog?

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

How to send logs from a python application to Datadog using ddtrace?

问题

让我们假设我有一个使用cron定期运行的Python例程。现在,假设我想将其日志发送到Datadog。我认为最简单的方法是通过Datadog的代理,例如使用ddtrace...

import ddtrace

ddtrace.patch_all()

import logging

logger = logging.getLogger(__name__)
logger.warning("Dummy log")

...但这并不起作用。我尝试过使用DD_LOGS_INJECTION=trueDD_LOGS_ENABLED=true,但根据文档,似乎我需要配置一些内容,以便代理程序会追踪日志文件。但是,根据type: file,我猜我可以发送日志而无需担心创建这些配置文件。

你会说什么是将日志发送到Datadog的最简单方法,以及如何从Python应用程序中执行这个操作?

英文:

Let's say that I have a python routine that runs periodically using cron. Now, let's say that I want to send logs from it to Datadog. I thought that the simples way to do it would be via Datadog's agent, e.g. using ddtrace...

import ddtrace

ddtrace.patch_all()

import logging

logger = logging.getLogger(__name__)
logger.warning("Dummy log")

...but this is not working. I've tried with both DD_LOGS_INJECTION=true and DD_LOGS_ENABLED=true but looking at the docs it seems that I have to configure something so the Agent will tail the log files. However, by looking at type: file I'd guess that I could send logs without having to worry with creating those configuration files.

What would you say is the simplest way to send logs do Datadog and how to do that from a python application?

答案1

得分: 0

DD_LOGS_ENABLED没有效果。我不得不直接编辑配置文件为logs_enabled: true

英文:

It turned out that DD_LOGS_ENABLED has no effect. I had to directly edit the configuration file to logs_enabled: true.

答案2

得分: -1

你需要设置你的API密钥和应用密钥:

datadog.initialize(
    api_key='YOUR_API_KEY',
    app_key='YOUR_APP_KEY',
    logs_enabled=True)
英文:

You need to set your API key and app key:

datadog.initialize(
    api_key='YOUR_API_KEY',
    app_key='YOUR_APP_KEY',
    logs_enabled=True)

huangapple
  • 本文由 发表于 2023年5月25日 04:15:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327116.html
匿名

发表评论

匿名网友

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

确定