---
title: Correlating Go Logs and Traces
description: Connect your Go logs and traces to correlate them in Datadog.
breadcrumbs: >-
  Docs > APM > Correlate APM Data with Other Telemetry > Correlate Logs and
  Traces > Correlating Go Logs and Traces
---

# Correlating Go Logs and Traces

**Note**: This documentation uses v2 of the Go tracer, which Datadog recommends for all users. If you are using v1, see the [migration guide](https://docs.datadoghq.com/tracing/trace_collection/custom_instrumentation/go/migration) to upgrade to v2.

## Manual injection{% #manual-injection %}

The Go tracer API allows printing span information along with log statements using the `%v` format specifier:

```go
package main

import (
    "net/http"

    "github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
)

func handler(w http.ResponseWriter, r *http.Request) {
    // Create a span for a web request at the /posts URL.
    span := tracer.StartSpan("web.request", tracer.ResourceName("/posts"))
    defer span.Finish()

    // Append span info to log messages:
    log.Printf("my log message %v", span)
}
```

The above example illustrates how to use the span's context in the standard library's `log` package. Similar logic may be applied to third party packages too.

**Note**: If you are not using a [Datadog Log Integration](https://docs.datadoghq.com/logs/log_collection/go/#configure-your-logger) to parse your logs, custom log parsing rules need to ensure that `dd.trace_id`, `dd.span_id`, `dd.service`, `dd.env` and `dd.version` are being parsed as strings. More information can be found in [Correlated Logs Not Showing Up in the Trace ID Panel](https://docs.datadoghq.com/tracing/troubleshooting/correlated-logs-not-showing-up-in-the-trace-id-panel/?tab=custom).

## Injection into logrus logs{% #injection-into-logrus-logs %}

A hook for the logrus package is available to automatically link your log and spans.

```go
package main

import (
    "github.com/sirupsen/logrus"

    dd_logrus "github.com/DataDog/dd-trace-go/contrib/sirupsen/logrus/v2"
    "github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
)

func main() {
    // Optional: Change log format to use JSON (Cf. Go Log Collection)
    logrus.SetFormatter(&logrus.JSONFormatter{})

    // Add Datadog context log hook
    logrus.AddHook(&dd_logrus.DDContextLogHook{}) 
    
    // ...
}
```

This automatically injects the trace id to your logs when you log with the context.

```go
    // Log with the context
    logrus.WithContext(ctx).Info("Go logs and traces connected!")
```

## Further Reading{% #further-reading %}

- [Manually instrument your application to create traces.](https://docs.datadoghq.com/tracing/trace_collection/custom_instrumentation)
- [Explore your services, resources, and traces](https://docs.datadoghq.com/tracing/glossary/)
- [Correlate request logs with traces automatically](https://www.datadoghq.com/blog/request-log-correlation/)
- [Ease troubleshooting with cross product correlation.](https://docs.datadoghq.com/logs/guide/ease-troubleshooting-with-cross-product-correlation/)
