# \[Python\] How to send logs to Elasticsearch?

**URL:** <https://discuss.elastic.co/t/python-how-to-send-logs-to-elasticsearch/373376>\
**Category:** APM\
**Created:** [January 20, 2025, 3:56am UTC](https://discuss.elastic.co/t/python-how-to-send-logs-to-elasticsearch/373376 "2025-01-20T03:56:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shane\_Chang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shane_chang/32/140729_2.png) [@Shane\_Chang](https://discuss.elastic.co/u/Shane_Chang)\
**Post date:** [January 20, 2025, 3:56am UTC](https://discuss.elastic.co/t/python-how-to-send-logs-to-elasticsearch/373376/1 "2025-01-20T03:56:53Z")

</div>

Hi Guys, I got some questions. Here is my use case to be solved.

Python version: 3.11.4  
Elastic version: 8.11.3  
Python packages:  
fastapi = "0.103.1"  
elastic-apm = "6.23.0"

I am maintaining a Python FastAPI server which is using APM package "elastic-apm 6.23.0" to trace API spans.  
Before, we had used standard library "logging.FileHandler" to record logs in a log file locally then transfer logs from local to Elasticsearch by Elastic Agent. Because we mark tracing information tailing the logs, we can directly see correlated logs under the logs tab.

And now we are trying to transfer logs without writing local file.  
Thus, I tried "elasticapm.handlers.logging.LoggingHandler" to send logs to APM server. I also can see correlated logs on Kibana APM UI.

However, the function "capture" hardcoded "error" category, which is one of the distinguishable category of APM server, and cause NULL type error being showed on APM Error tab.

 ![](https://us1.discourse-cdn.com/elastic/original/3X/c/a/caca9b65acb67a4c886f19d4ec1e56b7878e2603.png)  
This confused me. I just want to make correlated logs showed alongside transaction/tracing tab.

I also tried other methods to achieve that but not really worked till now.  
Like directly sending messages to Elasticsearch via HTTP calls, messages only can be found in Kibana Discover, couldn't be visualized in Kibana Logs and APM logs tab.

Does anyone have ideas about this?

---

<div class="post-metadata">

**Author:** ![TimV](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timv/32/13162_2.png) [@TimV](https://discuss.elastic.co/u/TimV)\
**Post date:** [January 20, 2025, 5:47am UTC](https://discuss.elastic.co/t/python-how-to-send-logs-to-elasticsearch/373376/2 "2025-01-20T05:47:57Z")

</div>

I moved your question to the **APM** forum because I think it needs an answer from the `elasticapm` team.

---

<div class="post-metadata">

**Author:** ![ashishtiwari1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashishtiwari1993/32/135241_2.png) [@ashishtiwari1993](https://discuss.elastic.co/u/ashishtiwari1993)\
**Post date:** [January 20, 2025, 6:53am UTC](https://discuss.elastic.co/t/python-how-to-send-logs-to-elasticsearch/373376/3 "2025-01-20T06:53:03Z")

</div>

HI @Shane_Chang , Welcome to the Elastic community.

A note from the [doc](https://www.elastic.co/guide/en/apm/agent/python/current/logs.html) -

> Elastic Python APM Agent does not send the logs to Elasticsearch. It only injects correlation IDs and reformats the logs. You must use another ingestion strategy. We recommend [Filebeat](https://www.elastic.co/beats/filebeat) for that purpose.

`trace.id`, `transcation.id` & `span.id` is getting injected in your logs? If you can share the snippet.

Also you can give a try to [structlog](https://www.elastic.co/guide/en/apm/agent/python/current/logs.html#structlog) which will add above ids.

Once it visible in your file log you need to use [elastic agent](https://www.elastic.co/guide/en/fleet/current/install-standalone-elastic-agent.html) or [filebeat](https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-installation-configuration.html) to read the same logs and push to Elasticsearch.
