To send your Go logs to Datadog, log to a file and then tail that file with your Datadog Agent. You can use the following setup with logrus, an open source logging library.
Datadog strongly encourages setting up your logging library to produce your logs in JSON to avoid the need for custom parsing rules.
For a classic Go configuration, open a main.go file and paste in the following code:
packagemainimport(log"github.com/sirupsen/logrus")funcmain(){// use JSONFormatter
log.SetFormatter(&log.JSONFormatter{})// log an event as usual with logrus
log.WithFields(log.Fields{"string":"foo","int":1,"float":1.1}).Info("My first event from golang to stdout")}
You can add metas to any log if you provide a JSON object that you want to see in the log event.
These metas can be hostname, username, customers, metric or any information that can help you troubleshoot and understand what happens in your Go application.
packagemainimport(log"github.com/sirupsen/logrus")funcmain(){// use JSONFormatter
log.SetFormatter(&log.JSONFormatter{})// log an event with logrus
log.WithFields(log.Fields{"string":"foo","int":1,"float":1.1}).Info("My first event from golang to stdout")// for metadata, a common pattern is to reuse fields between logging statements by reusing
contextualizedLog:=log.WithFields(log.Fields{"hostname":"staging-1","appname":"foo-app","session":"1ce3f6v"})contextualizedLog.Info("Simple event with global metadata")}
Run the Agent’s status subcommand and look for go under the Checks section to confirm logs are successfully submitted to Datadog.
If logs are in JSON format, Datadog automatically parses the log messages to extract log attributes. Use the Log Explorer to view and troubleshoot your logs.
If APM is enabled for this application, the correlation between application logs and traces can be improved by following the APM Go logging documentation to automatically add trace and span IDs in your logs.