Skip to content

DataDog Transport Server

NPM Version

Ships logs server-side to Datadog using the datadog-transport-common library.

Transport Source

Important Notes

  • Only works server-side (not in browsers)
  • You will not get any console output since this sends directly to DataDog. Use the onDebug option to log out messages.

Installation

Install the required packages (datadog-transport-common is installed as part of @loglayer/transport-datadog):

sh
npm i loglayer @loglayer/transport-datadog serialize-error
sh
pnpm add loglayer @loglayer/transport-datadog serialize-error
sh
yarn add loglayer @loglayer/transport-datadog serialize-error

Usage Example

DataDog Error Tracking

To use DataDog's Error Tracking feature, configure the errorFieldName as error in your LogLayer configuration.

Alternatively, you can use DataDog's Pipeline Processing to remap the default err field to error.

You can also add remapping via DataDog Logs > Configuration > Standard Attributes and add remapping for the error.message / error.stack attributes (or add a new one for these if they do not exist) and remap to how you've configured LogLayer.

For example, attribute error.message might remap to err.message,metadata.err.message, depending on how LogLayer is configured.

typescript
import { LogLayer } from 'loglayer'
import { DataDogTransport } from "@loglayer/transport-datadog"
import { serializeError } from "serialize-error";

const log = new LogLayer({
  errorSerializer: serializeError,
  errorFieldName: "error",
  transport: new DataDogTransport({
    options: {
      ddClientConf: {
        authMethods: {
          apiKeyAuth: "YOUR_API_KEY",
        },
      },
      ddServerConf: {
        // Note: This must match the site you use for your DataDog login - See below for more info
        site: "datadoghq.eu"
      },
      onDebug: (msg) => {
        console.log(msg);
      },
      onError: (err, logs) => {
        console.error(err, logs);
      },
    },
  })
})

Transport Configuration

Required Parameters

NameTypeDescription
optionsDDTransportOptionsThe options for the transport

Optional Parameters

NameTypeDefaultDescription
enabledbooleantrueWhether the transport is enabled
messageFieldstring"message"The field name to use for the message
levelFieldstring"level"The field name to use for the log level
timestampFieldstring"time"The field name to use for the timestamp
timestampFunction() => any-A custom function to stamp the timestamp. The default timestamp uses the ISO 8601 format
level"trace" | "debug" | "info" | "warn" | "error" | "fatal""trace"Minimum log level to process. Logs below this level will be filtered out

Changelog

View the changelog here.