DataDog Browser Logs Transport Browser
@datadog/browser-logs is Datadog's official browser-side logging library.
Important Notes
- Only works in browser environments (not in Node.js)
- For server-side logging, use the
@loglayer/transport-datadogpackage
- For server-side logging, use the
- You will not get any console output since this sends directly to DataDog. Use the
onDebugoption to log out messages.
Installation
Install the required packages:
sh
npm i loglayer @loglayer/transport-datadog-browser-logs @datadog/browser-logssh
pnpm add loglayer @loglayer/transport-datadog-browser-logs @datadog/browser-logssh
yarn add loglayer @loglayer/transport-datadog-browser-logs @datadog/browser-logsSetup
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
| Name | Type | Description |
|---|---|---|
logger | datadogLogs.logger | The DataDog browser logs instance |
Optional Parameters
| Name | Type | Default | Description |
|---|---|---|---|
enabled | boolean | true | Whether 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
| LogLayer | Datadog |
|---|---|
| trace | debug |
| debug | debug |
| info | info |
| warn | warn |
| error | error |
| fatal | error |
Changelog
View the changelog here.
