Customize logger
This topic describes how to customize the log information about experiments to help with debugging.
The logger logs information about your experiments to help you with debugging. You can customize where log information is sent and what kind of information is tracked.
The JavaScript SDK comes with a default Logger
implementation set to the ERROR
log level. To configure the log level threshold, you can call setLogLevel
on the SDK.
var optimizelySDK = require('@optimizely/optimizely-sdk');
// Set log level to debug
// Can be 'info', 'debug', 'warn', 'error'
optimizelySDK.setLogLevel('debug');
// To turn off logging, call setLogger with null
optimizelySDK.setLogger(null);
For finer control over your SDK configuration in a production environment, pass in a custom logger for your Optimizely client. A custom logger is a function that takes two arguments, the level, and the message. See the code example below to create and set a custom logger.
var optimizelySDK = require('@optimizely/optimizely-sdk');
// Set log level
optimizelySDK.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
*
*/
var customLogger = function(level, message) {
var LOG_LEVEL = optimizelySDK.enums.LOG_LEVEL;
switch (level) {
case LOG_LEVEL.INFO:
// INFO log message
console.log(message);
break;
case LOG_LEVEL.DEBUG:
// DEBUG log message
console.log(message);
break;
case LOG_LEVEL.WARNING:
// WARNING log message
console.log(message);
break;
case LOG_LEVEL.ERROR:
// ERROR log message
console.log(message);
break;
}
}
// Set the custom logger
optimizelySDK.setLogger({
log: customLogger,
});
Log levels
The table below lists the log levels for the JavaScript SDK.
Log Level | Explanation |
---|---|
optimizelySDK.enums.LOG_LEVEL.ERROR | Events that prevent feature flags or experiments from functioning correctly (for example, invalid datafile in initialization and invalid feature keys) are logged. The user can take action to correct. |
optimizelySDK.enums.LOG_LEVEL.WARNING | Events that don't prevent feature flags or experiments from functioning correctly, but can have unexpected outcomes (for example, future API deprecation, logger or error handler are not set properly) are logged. |
optimizelySDK.enums.LOG_LEVEL.INFO | Events of significance (for example, activate started, activate succeeded, tracking started, and tracking succeeded) are logged. This is helpful in showing the lifecycle of an API call. |
optimizelySDK.enums.LOG_LEVEL.DEBUG | Any information related to errors that can help us debug the issue (for example, the feature flag is not running, user is not included in the rollout) are logged. |
Updated over 2 years ago