Syntax
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
Browser Pro, or Pro+SPA agent (v1.261.0 or higher)
If you're using npm to install the browser agent and using a non-standard implementation, you must enable the
logging
feature when instantiating theBrowserAgent
class. For example, add the following in thefeatures
array: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.
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 for more information about log events.
Optional configurations can be passed along with these captured logs with the options
argument. Any custom attributes supplied to the API call in the options
argument (options.customAttributes
) will be appended as top-level attributes on every log event created by this wrapper. You can control the level
of the captured log by supplying a level
to the options
argument (options.level
), which defaults to info
. Note that once successfully wrapped, the function's logging detection can't be altered.
Parameters
Parameter | Description |
---|---|
Object | Required. An object which contains the target function to be wrapped. |
string | Required. The name of the target function to be wrapped. This function must exist in the |
Object | Optional. An object used for supplying optional configurations for every log captured by the wrapper. |
Examples
Capturing log items from the native console method(s)
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
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
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
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
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'