---
title: Use APM agent APIs with logs in context
source: https://docs.newrelic.com/docs/logs/logs-context/annotate-logs-logs-context-using-apm-agent-apis
---

To correlate log data with other telemetry data, such as errors and distributed traces in APM, you can use [APM logs in context](https://docs.newrelic.com/docs/apm/new-relic-apm/getting-started/get-started-logs-context). If your logging framework is not supported by our existing logs in context solutions, you can configure your logging libraries with our APM agent APIs.

## APM agent trace metadata and linking metadata APIs [#apis]

To get properly annotated logs for logs in context, use the following API calls for your APM agent. These APIs pass the required trace metadata and linking metadata to link your log data to other New Relic data.

APM agent APIs:

| APM agent | API calls                                                                                                                                                                                                                                                                                                                                                                            |
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Go        | - [`GetTraceMetadata`](https://pkg.go.dev/github.com/newrelic/go-agent/v3/newrelic#Transaction.GetTraceMetadata) - [`GetLinkingMetadata`](https://pkg.go.dev/github.com/newrelic/go-agent/v3/newrelic#Transaction.GetLinkingMetadata)                                                                                                                                                |
| Java      | - [`getTraceMetadata`](http://newrelic.github.io/java-agent-api/javadoc/com/newrelic/api/agent/Agent.html#getTraceMetadata()) - [`getLinkingMetadata`](http://newrelic.github.io/java-agent-api/javadoc/com/newrelic/api/agent/Agent.html#getLinkingMetadata())                                                                                                                      |
| .NET      | - [`TraceMetadata`](https://docs.newrelic.com/docs/apm/agents/net-agent/net-agent-api/net-agent-api/#TraceMetadata) - [`GetLinkingMetadata`](https://docs.newrelic.com/docs/apm/agents/net-agent/net-agent-api/net-agent-api/#GetLinkingMetadata)                                                                                                                                    |
| Node.js   | - [`newrelic.getTraceMetadata`](https://newrelic.github.io/node-newrelic/API.html#getTraceMetadata) - [`newrelic.getLinkingMetadata`](https://newrelic.github.io/node-newrelic/API.html#getLinkingMetadata)                                                                                                                                                                          |
| PHP       | - [`newrelic_get_trace_metadata`](https://docs.newrelic.com/docs/apm/agents/php-agent/php-agent-api/newrelicgettracemetadata/) - [`newrelic_get_linking_metadata`](https://docs.newrelic.com/docs/apm/agents/php-agent/php-agent-api/newrelicgetlinkingmetadata/) For PHP, logs in context is only supported from the distributed tracing UI, not in the **Logs** tab of the APM UI. |
| Python    | - [`get_linking_metadata`](https://docs.newrelic.com/docs/apm/agents/python-agent/python-agent-api/getlinkingmetadata-python-agent-api/)                                                                                                                                                                                                                                             |
| Ruby      | - [`linking_metadata`](https://www.rubydoc.info/gems/newrelic_rpm/NewRelic/Agent#linking_metadata-instance_method) - [`current_trace_id`](https://www.rubydoc.info/gems/newrelic_rpm/NewRelic/Agent/Tracer#current_trace_id-class_method) - [`current_span_id`](https://www.rubydoc.info/gems/newrelic_rpm/NewRelic/Agent/Tracer#current_span_id-class_method)                       |

## Resources for correctly annotating logs [#tips]

For more information about using the [trace metadata and linking metadata APIs](#apis) to annotate logs for logs in context, review the [APM agent specifications in GitHub](https://github.com/newrelic/newrelic-exporter-specs/tree/master/logging). These specifications include the required fields and properly formatted output.

Also, review the source code for our own manually installed logs in context extensions to see how we use these APIs:

-   Go: [Logs In Context Extensions](https://github.com/newrelic/go-agent/tree/master/v3/integrations/logcontext-v2)
-   Java: [Log4j2 extension](https://github.com/newrelic/java-log-extensions/blob/master/log4j2/src/main/java/com/newrelic/logging/log4j2/NewRelicLayout.java)
-   .NET: [Serilog extension](https://github.com/newrelic/newrelic-logenricher-dotnet/tree/main/src/NewRelic.LogEnrichers.Serilog)
-   Node.js: [Winston extension](https://github.com/newrelic/newrelic-node-log-extensions/blob/main/packages/winston-log-enricher/lib/createFormatter.js), [Pino extension](https://github.com/newrelic/newrelic-node-log-extensions/blob/main/packages/pino-log-enricher/lib/createFormatter.js)
-   PHP: [Monolog extension](https://github.com/newrelic/newrelic-monolog-logenricher-php/blob/master/src/Formatter.php)
-   Python: [Streamhandler example](https://docs.newrelic.com/docs/logs/logs-context/configure-logs-context-python/#3-old-formatter)
-   Ruby: [logging.rb extension](https://github.com/newrelic/newrelic-ruby-agent/blob/dev/lib/new_relic/agent/logging.rb)
