Skip to content

Update logger Global Context APIs #1695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 24, 2022
67 changes: 54 additions & 13 deletions packages/logs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,18 @@ if (window.DD_LOGS) {

After the Datadog browser logs SDK is initialized, it is possible to:

- Set the entire context for all your loggers with the `setLoggerGlobalContext (context: Context)` API.
- Add a context to all your loggers with `addLoggerGlobalContext (key: string, value: any)` API.
- Get the entire global context with `getLoggerGlobalContext ()` API.
- Set the entire context for all your loggers with the `setGlobalContext (context: object)` API.
- Add a context to all your loggers with the `setGlobalContextProperty (key: string, value: any)` API.
- Get the entire global context with the `getGlobalContext ()` API.
- Remove context property with the `removeGlobalContextProperty (key: string)` API.
- Clear all existing context properties with the `clearGlobalContext ()` API.

> The Log Browser SDK v4.17.0 has updated the names of several APIs:
>
> - `getGlobalContext` instead of `getLoggerGlobalContext`
> - `setGlobalContext` instead of `setLoggerGlobalContext`
> - `setGlobalContextProperty` instead of `addLoggerGlobalContext`
> - `removeGlobalContextProperty` instead of `removeLoggerGlobalContext`

##### NPM

Expand All @@ -490,11 +499,19 @@ For NPM, use:
```javascript
import { datadogLogs } from '@datadog/browser-logs'

datadogLogs.setLoggerGlobalContext({ env: 'staging' })
datadogLogs.setGlobalContext({ env: 'staging' })

datadogLogs.addLoggerGlobalContext('referrer', document.referrer)
datadogLogs.setGlobalContextProperty('referrer', document.referrer)

const context = datadogLogs.getLoggerGlobalContext() // => {env: 'staging', referrer: ...}
datadogLogs.getGlobalContext() // => {env: 'staging', referrer: ...}

datadogLogs.removeGlobalContextProperty('referrer')

datadogLogs.getGlobalContext() // => {env: 'staging'}

datadogLogs.clearGlobalContext()

datadogLogs.getGlobalContext() // => {}
```

#### CDN async
Expand All @@ -503,15 +520,31 @@ For CDN async, use:

```javascript
DD_LOGS.onReady(function () {
DD_LOGS.setLoggerGlobalContext({ env: 'staging' })
DD_LOGS.setGlobalContext({ env: 'staging' })
})

DD_LOGS.onReady(function () {
DD_LOGS.setGlobalContextProperty('referrer', document.referrer)
})

DD_LOGS.onReady(function () {
DD_LOGS.getGlobalContext() // => {env: 'staging', referrer: ...}
})

DD_LOGS.onReady(function () {
DD_LOGS.addLoggerGlobalContext('referrer', document.referrer)
DD_LOGS.removeGlobalContextProperty('referrer')
})

DD_LOGS.onReady(function () {
var context = DD_LOGS.getLoggerGlobalContext() // => {env: 'staging', referrer: ...}
DD_LOGS.getGlobalContext() // => {env: 'staging'}
})

DD_LOGS.onReady(function () {
DD_LOGS.clearGlobalContext()
})

DD_LOGS.onReady(function () {
DD_LOGS.getGlobalContext() // => {}
})
```

Expand All @@ -522,11 +555,19 @@ DD_LOGS.onReady(function () {
For CDN sync, use:

```javascript
window.DD_LOGS && DD_LOGS.setLoggerGlobalContext({ env: 'staging' })
window.DD_LOGS && DD_LOGS.setGlobalContext({ env: 'staging' })

window.DD_LOGS && DD_LOGS.setGlobalContextProperty('referrer', document.referrer)

window.DD_LOGS && DD_LOGS.getGlobalContext() // => {env: 'staging', referrer: ...}

window.DD_LOGS && DD_LOGS.removeGlobalContextProperty('referrer')

window.DD_LOGS && DD_LOGS.getGlobalContext() // => {env: 'staging'}

window.DD_LOGS && DD_LOGS.addLoggerGlobalContext('referrer', document.referrer)
window.DD_LOGS && DD_LOGS.clearGlobalContext()

var context = window.DD_LOGS && DD_LOGS.getLoggerGlobalContext() // => {env: 'staging', referrer: ...}
window.DD_LOGS && DD_LOGS.getGlobalContext() // => {}
```

**Note**: The `window.DD_LOGS` check is used to prevent issues if a loading failure occurs with the SDK.
Expand All @@ -535,7 +576,7 @@ var context = window.DD_LOGS && DD_LOGS.getLoggerGlobalContext() // => {env: 'st

After a logger is created, it is possible to:

- Set the entire context for your logger with the `setContext (context: Context)` API.
- Set the entire context for your logger with the `setContext (context: object)` API.
- Add a context to your logger with `addContext (key: string, value: any)` API:

##### NPM
Expand Down