Skip to content

Commit

Permalink
Update logger Global Context APIs (#1695)
Browse files Browse the repository at this point in the history
* Update logger Global Context APIs

* fix README format

* Update packages/logs/README.md

Co-authored-by: Heston Hoffman <hestonhoffman@gmail.com>

* Update packages/logs/README.md

Co-authored-by: Heston Hoffman <hestonhoffman@gmail.com>

* Document  `removeGlobalContextProperty` and `clearGlobalContext`

* change type Context to object

* use DD_LOGS instead of datadogLogs

Co-authored-by: Heston Hoffman <hestonhoffman@gmail.com>
  • Loading branch information
liywjl and hestonhoffman authored Aug 24, 2022
1 parent 0169b4b commit d62c738
Showing 1 changed file with 54 additions and 13 deletions.
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

0 comments on commit d62c738

Please sign in to comment.