Skip to content

DataDog Browser Logs Transport Browser

NPM Version

@datadog/browser-logs is Datadog's official browser-side logging library.

Transport Source

Important Notes

  • Only works in browser environments (not in Node.js)
  • 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:

sh
npm i loglayer @loglayer/transport-datadog-browser-logs @datadog/browser-logs
sh
pnpm add loglayer @loglayer/transport-datadog-browser-logs @datadog/browser-logs
sh
yarn add loglayer @loglayer/transport-datadog-browser-logs @datadog/browser-logs

Setup

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 { datadogLogs } from '@datadog/browser-logs'
import { LogLayer } from 'loglayer'
import { DataDogBrowserLogsTransport } from "@loglayer/transport-datadog-browser-logs"

// Initialize Datadog
datadogLogs.init({
  clientToken: '<CLIENT_TOKEN>',
  site: '<DATADOG_SITE>',
  forwardErrorsToLogs: true,
  sampleRate: 100
})

// Basic setup
const log = new LogLayer({
  errorFieldName: "error",
  transport: new DataDogBrowserLogsTransport({
    logger: datadogLogs.logger
  })
})

// Or with a custom logger instance
const logger = datadogLogs.createLogger('my-logger')

const log = new LogLayer({
  transport: new DataDogBrowserLogsTransport({
    errorFieldName: "error",
    logger
  })
})

Configuration Options

Required Parameters

NameTypeDescription
loggerdatadogLogs.loggerThe DataDog browser logs instance

Optional Parameters

NameTypeDefaultDescription
enabledbooleantrueWhether the transport is enabled
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.

Log Level Mapping

LogLayerDatadog
tracedebug
debugdebug
infoinfo
warnwarn
errorerror
fatalerror

Changelog

View the changelog here.