Customize the JavaScript (Node) SDK prior to v6 logger
Customize log information from the Optimizely Feature Experimentation JavaScript (Node) SDK for debugging experiments.
The logger captures details about your experiments, making it easier for you to debug them. You can customize where the log information is stored and choose the types of data you want to track.
The JavaScript SDK comes with a default Logger
implementation set to the ERROR
log level. To configure the log-level threshold, call setLogLevel
on the SDK.
const { setLogLevel, setLogger, logging } = require('@optimizely/optimizely-sdk');
// Set log level to debug
// Can be 'info', 'debug', 'warn', 'error'
setLogLevel('debug');
setLogger(logging.createLogger());
// To turn off logging, call setLogger with null
setLogger(null);
Important
When using the default logger: in the latest version of Node SDK, you need to manually create the logger. Use the following line to create and set a logger:
// create logger setLogger(logging.createLogger());
Pass in a custom logger for your Optimizely client for finer control over your SDK configuration in a production environment. A custom logger is a function that receives two arguments: the log level and the message. See the following code sample to create and set a custom logger.
const { setLogLevel, setLogger, enums } = require('@optimizely/optimizely-sdk');
// Set log level
setLogLevel('debug');
/**
* customLogger
*
* Example of a custom logger. A custom logger is a function that
* takes two parameters (level, message) and logs to an appropriate place,
* typically the console.
*
* @param {string} level indicating the level of the log message
* @param {string} message indicating the log message
*
*/
const customLogger = (level, message) => {
var LOG_LEVEL = enums.LOG_LEVEL;
switch (level) {
case LOG_LEVEL.INFO:
// INFO log message
console.info(message);
break;
case LOG_LEVEL.DEBUG:
// DEBUG log message
console.debug(message);
break;
case LOG_LEVEL.WARNING:
// WARNING log message
console.warn(message);
break;
case LOG_LEVEL.ERROR:
// ERROR log message
console.error(message);
break;
}
}
// Set the custom logger
setLogger({
log: customLogger,
});
Log levels
The following list shows the log levels for the JavaScript (Node) SDK.
- optimizelySDK.enums.LOG_LEVEL.ERROR – Logs events that prevent feature flags from working properly, such as an invalid data file during initialization or incorrect feature keys. These issues require user intervention to fix.
- optimizelySDK.enums.LOG_LEVEL.WARN – Logs events that do not prevent feature flags from working properly but may cause unexpected results, such as future API deprecation, improper logger or error handler settings, and nil values from getters.
- optimizelySDK.enums.LOG_LEVEL.INFO – Logs key events to illustrate the lifecycle of an API call, such as when a decision or tracking starts and succeeds.
- optimizelySDK.enums.LOG_LEVEL.DEBUG – Logs extra information related to errors that aid Optimizely in debugging, like when a feature flag is not running or a user is not included in a rollout.
Updated 9 days ago