• /
  • EnglishEspañol日本語한국어Português
  • Inicia sesiónComenzar ahora

wrapLogger

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 the BrowserAgent 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

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

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'
Copyright © 2024 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.