Docker Log collection
Security Monitoring is now available Security Monitoring is now available

Docker Log collection

Overview

Datadog Agent 6+ collects logs from containers. Two types of installation are available:

  • On the host, where the Agent is external to the Docker environment
  • By deploying the containerized Agent in the Docker environment

Then, collect all the logs from your environment’s containers, or filter by container image, name, or container label to choose the logs collected. This documentation discusses how to collect logs from all running containers, as well as how to leverage Autodiscovery to activate log integrations.

One-step install

The first step is to install the Agent (whether the containerized version or directly on the host) and to enable log collection for all the containers.

To run a Docker container that embeds the Datadog Agent to monitor your host, use the following command:

docker run -d --name datadog-agent \
           -e DD_API_KEY="<DATADOG_API_KEY>" \
           -e DD_LOGS_ENABLED=true \
           -e DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL=true \
           -e DD_CONTAINER_EXCLUDE="name:datadog-agent" \
           -v /var/run/docker.sock:/var/run/docker.sock:ro \
           -v /proc/:/host/proc/:ro \
           -v /opt/datadog-agent/run:/opt/datadog-agent/run:rw \
           -v /sys/fs/cgroup/:/host/sys/fs/cgroup:ro \
           datadog/agent:latest

Note: On Windows systems, run this command without volume mounts. That is:

docker run -d --name datadog-agent \
           -e DD_API_KEY="<DATADOG_API_KEY>" \
           -e DD_LOGS_ENABLED=true \
           -e DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL=true \
           -e DD_CONTAINER_EXCLUDE="name:datadog-agent" \
           -v \\.\pipe\docker_engine:\\.\pipe\docker_engine \
           datadog/agent:latest

It is recommended that you pick the latest version of the Datadog Agent. Consult the full list of available images for Agent v6 on Docker Hub.

The commands related to log collection are:

CommandDescription
-e DD_LOGS_ENABLED=trueEnables log collection when set to true. The Agent looks for log instructions in configuration files.
-e DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL=trueAdds a log configuration that enables log collection for all containers.
-v /opt/datadog-agent/run:/opt/datadog-agent/run:rwTo prevent loss of container logs during restarts or network issues, the last log line collected for each container in this directory is stored on the host.
-e DD_CONTAINER_EXCLUDE="name:datadog-agent"Prevents the Datadog Agent from collecting and sending its own logs and metrics. Remove this parameter if you want to collect the Datadog Agent logs or metrics.
-v /var/run/docker.sock:/var/run/docker.sock:roLogs are collected from container stdout/stderr from the Docker socket.

Install the latest version of Agent 6 on your host. The Agent can collect logs from files on the host or from container stdout/stderr.

Collecting logs is disabled by default in the Datadog Agent. To enable it, add the following lines in your datadog.yaml configuration file:

logs_enabled: true
listeners:
    - name: docker
config_providers:
    - name: docker
      polling: true
logs_config:
    container_collect_all: true

Restart the Agent to see all your container logs in Datadog.

Important notes:

  • source and service default to the short_image tag value in Datadog Agent 6.8+. The source and service values can be overridden with Autodiscovery as described below. Setting the source value to an integration name results in the installation of integration Pipelines that parse your logs and extract relevant information from them.

  • Logs coming from container Stderr have a default status of Error.

  • If using the journald logging driver instead of Docker’s default json-file logging driver, see the journald integration documentation for details regarding the setup for containerized environments.

Log Integrations

In Datadog Agent 6.8+, source and service default to the short_image tag value. This allows Datadog to identify the log source for each container and automatically install the corresponding integration.

The container short image name might not match the integration name for custom images, and can be overwritten to better reflect the name of your application. This can be done with Datadog Autodiscovery and pod annotations in Kubernetes or container labels.

Autodiscovery expects labels to follow this format, depending on the file type:

Add the following LABEL to your Dockerfile:

LABEL "com.datadoghq.ad.logs"='[<LOGS_CONFIG>]'

Add the following label in your docker-compose.yaml file:

labels:
    com.datadoghq.ad.logs: '["<LOGS_CONFIG>"]'

Add the following label as a run command:

-l com.datadoghq.ad.logs='[<LOGS_CONFIG>]'

Where <LOG_CONFIG> is the log collection configuration you would find inside an integration configuration file. See log collection configuration to learn more.

Note: When configuring the service value through docker labels, Datadog recommends using unified service tagging as a best practice. Unified service tagging ties all Datadog telemetry together, including logs, through the use of three standard tags: env, service, and version. To learn how to configure your environment with unified tagging, refer to the dedicated unified service tagging documentation.

Examples

The following Dockerfile enables the NGINX log integration on the corresponding container (service value can be changed):

LABEL "com.datadoghq.ad.logs"='[{"source": "nginx", "service": "webapp"}]'

To enable both the metric and logs NGINX integrations:

LABEL "com.datadoghq.ad.check_names"='["nginx"]'
LABEL "com.datadoghq.ad.init_configs"='[{}]'
LABEL "com.datadoghq.ad.instances"='[{"nginx_status_url": "http://%%host%%:%%port%%/nginx_status"}]'
LABEL "com.datadoghq.ad.logs"='[{"source": "nginx", "service": "webapp"}]'

For multi-line logs like stack traces, the Agent has multi-line processing rules to aggregate lines into a single log.

Example log (Java stack traces):

2018-01-03T09:24:24.983Z UTC Exception in thread "main" java.lang.NullPointerException
        at com.example.myproject.Book.getTitle(Book.java:16)
        at com.example.myproject.Author.getBookTitles(Author.java:25)
        at com.example.myproject.Bootstrap.main(Bootstrap.java:14)

Use the com.datadoghq.ad.logs label as below on your containers to make sure that the above log is properly collected:

labels:
    com.datadoghq.ad.logs: '[{"source": "java", "service": "myapp", "log_processing_rules": [{"type": "multi_line", "name": "log_start_with_date", "pattern" : "\\d{4}-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])"}]}]'

See the multi-line processing rule documentation to get more pattern examples.

Note: Autodiscovery features can be used with or without the DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL environment variable. Choose one of the following options:

  • Use container labels or pod annotations to choose the containers to collect logs from.
  • Use the environment variable to collect logs from all containers and then override the default source and service values.
  • Add processing rules for the wanted subset of containers.

Advanced log collection

Use Autodiscovery log labels to apply advanced log collection processing logic, for example:

Filter containers

It is possible to manage from which containers you want to collect logs. This can be useful to prevent the collection of the Datadog Agent logs for instance. See the Container Discovery Management to learn more.

Short Lived containers

For a Docker environment, the Agent receives container updates in real time through Docker events. The Agent extracts and updates the configuration from the container labels (Autodiscovery) every 1 seconds.

Since Agent v6.14+, the Agent collects logs for all containers (running or stopped) which means that short lived containers logs that have started and stopped in the past second are still collected as long as they are not removed.

For Kubernetes environements, refer to the Kubernetes short lived container documentation

Further Reading