Skip to content
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

Update logger Global Context APIs #1695

Merged
merged 9 commits into from
Aug 24, 2022
63 changes: 51 additions & 12 deletions packages/logs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,16 @@ 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: Context)` API.
liywjl marked this conversation as resolved.
Show resolved Hide resolved
- Add a context to all your loggers with `setGlobalContextProperty (key: string, value: any)` API.
liywjl marked this conversation as resolved.
Show resolved Hide resolved
- Get the entire global context with `getGlobalContext ()` API.
liywjl marked this conversation as resolved.
Show resolved Hide resolved

> 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`
liywjl marked this conversation as resolved.
Show resolved Hide resolved

##### NPM

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

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

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

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

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

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

datadogLogs.clearGlobalContext()

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

#### CDN async
Expand All @@ -503,15 +518,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)
datadogLogs.removeGlobalContextProperty('referrer')
})

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

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

DD_LOGS.onReady(function () {
datadogLogs.getGlobalContext() // => {}
liywjl marked this conversation as resolved.
Show resolved Hide resolved
})
```

Expand All @@ -522,11 +553,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 Down