---
title: wrapLogger
source: https://docs.newrelic.com/docs/browser/new-relic-browser/browser-apis/wraplogger
---

## Syntax [#wrap-syntax]

```js
newrelic.wrapLogger(parent: Object, functionName: string, options?: Object<{ customAttributes?: Object, level?: 'debug|error|info|trace|warn'}>)
```

Automatically captures data passing through existing browser logging methods as log events.

## Requirements [#wrap-requirements]

-   Browser Pro, or Pro+SPA agent (v1.261.0 or higher)
-   If you're installing the browser agent via npm and building a custom agent with selected features, you must enable the `logging` feature when creating the `Agent` instance. For example, add the following in the`features` array:

    ```js
    import { Logging } from '@newrelic/browser-agent/features/logging'

    const options = {
      info: { ... },
      loader_config: { ... },
      init: { ... },
      features: [
        Logging
      ]
    }
    ```

For more information, see the [npm browser installation documentation](https://www.npmjs.com/package/@newrelic/browser-agent#new-relic-browser-agent).

## Description [#wrap-description]

After you provide this method with a valid parent container and child function name, the browser agent will record a new log event every time the wrapped function is invoked. The first argument is passed to the invoked function as the log's message. See the [Logs UI](https://docs.newrelic.com/docs/logs/ui-data/use-logs-ui/) for more information about log events.

You can pass the optional configurations along with these captured logs using the `options` argument. Any custom attributes supplied to the API call in the `options` argument (`options.customAttributes`) are appended as top-level attributes on every log event created by this wrapper and take precedence over any global custom attributes by [setCustomAttribute](https://docs.newrelic.com/docs/browser/new-relic-browser/browser-agent-spa-api/set-custom-attribute). Supply a `level` to the `options` argument (`options.level`) to control the `level` of captured log. By default, the log level is set to `info`.

## Parameters [#wrap-parameters]

| Parameter               | Description                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `parent` _Object_       | Required. An object which contains the target function to be wrapped.                                                                                                                                                                                                                                                                                                                                                                                    |
| `functionName` _string_ | Required. The name of the target function to be wrapped. This function must exist in the `parent` object and match the type of "function".                                                                                                                                                                                                                                                                                                               |
| `options` _Object_      | Optional. An object used for supplying optional configurations for every log captured by the wrapper. `options.customAttributes` is an object of key:val pairs that assigns a top-level property and value to the created log for each attribute supplied. The enum `options.level` assigns a log level to the created log event.  The `level` must be one of: `debug | error | info | trace | warn`.  The log level defaults to `info` if not supplied. |

## Examples [#wrap-examples]

### Capturing log items from the native console method(s) [#wrap-capture-log-items]

```js
newrelic.wrapLogger(console, 'info')
// from this point forward, every time `console.info` is invoked, it will save a log event with:
// a message of --> <the first argument passed to console.info>
// a level of --> 'info'
```

### Capturing log items from a custom logger [#wrap-capture-custom-logger]

```js
const myLoggers = {
  logger: function(){...}
}
newrelic.wrapLogger(myLoggers, 'logger')
// from this point forward, every time `myLoggers.logger` is invoked, it will save a log event with:
// a message of --> <the first argument passed to myLoggers.logger>
// a level of --> 'info'
```

### Capturing log items with a specified level [#wrap-specified-level]

```js
const myLoggers = {
  logger: function(){...}
}
newrelic.wrapLogger(myLoggers, 'logger', {level: 'debug'})
// from this point forward, every time `myLoggers.logger` is invoked, it will save a log event with:
// a message of --> <the first argument passed to myLoggers.logger>
// a level of --> 'debug'
```

### Capturing a log item with custom attributes [#wrap-custom-attributes]

```js
const myLoggers = {
  logger: function(){...}
}
newrelic.wrapLogger(myLoggers, 'logger', {customAttributes: {myFavoriteApp: true}})
// from this point forward, every time `myLoggers.logger` is invoked, it will save a log event with:
// a message of --> <the first argument passed to myLoggers.logger>
// a level of --> 'info'
// an attribute of --> 'myFavoriteApp: true'
```

### Wrap multiple loggers [#wrap-multiple-loggers]

```js
const myLoggers = {
  myInfoLogger: function(){...},
  myDebugLogger: function(){...}
}
newrelic.wrapLogger(myLoggers, 'myInfoLogger', {level: 'info'})
newrelic.wrapLogger(myLoggers, 'myDebugLogger', {level: 'debug'})
// from this point forward, every time `myLoggers.myInfoLogger` is invoked, it will save a log event with:
// a message of --> <the first argument passed to myLoggers.myInfoLogger>
// a level of --> 'info'

// every time `myLoggers.myDebugLogger` is invoked, it will save a log event with:
// a message of --> <the first argument passed to myLoggers.myDebugLogger>
// a level of --> 'debug'
```
