Logging
This topic describes how to use the SDK's logger in Optimizely Connect Platform (OCP) to assist in the development and debugging of your application.
To assist in the development and debugging, apps can log messages using the SDK's logger. These are only accessible by Optimizely and the app developer, not customers. Customer-facing messages must be provided via the Notifications interface instead.
You can log messages with one of the following log levels:
- debug
- info
- warning
- error
Thelogger
should be used in place of theconsole
to ensure that all messages are captured properly. However, as a safety measure for uncontrolled usages in dependencies and more, the app runtime capturesconsole
calls and writes the messages with the following log levels:
console function | logger level |
---|---|
debug | debug |
info | info |
log | info |
warn | warning |
error | error |
The logger uses the same mechanism that console.log
does to inspect objects passed in as arguments.
SDK
The SDK exposes the logging feature via the logger
instance, which can be imported as follows:
import {logger} from '@zaius/app-sdk';
The logger
instance follows the SDK's ILogger
interface:
export interface ILogger {
/**
* Write something to the logs at the Debug level
* @param args One or more values to log.
* Objects are formatted using `util.inspect`, other values are converted to a string.
* Multiple values are concatenated with a space between
*/
debug(...args: any[]): void;
/**
* Write something to the logs at the Info level
* @param args One or more values to log.
* Objects are formatted using `util.inspect`, other vaules are converted to a string.
* Multiple values are concatenated with a space between
*/
info(...args: any[]): void;
/**
* Write something to the logs at the Warning level
* @param args One or more values to log.
* Objects are formatted using `util.inspect`, other values are converted to a string.
* Multiple values are concatenated with a space between
*/
warn(...args: any[]): void;
/**
* Write something to the logs at the Error level
* @param args One or more values to log.
* Objects are formatted using `util.inspect`, other values are converted to a string.
* Multiple values are concatenated with a space between
*/
error(...args: any[]): void;
}
Stack Traces
In order to capture a stack trace, the error must be supplied as a separate argument. For example:
// This will include the stack trace of `e` if it is an error
logger.error('Something went wrong:', e);
// This will NOT include the stack trace
logger.error(`Something went wrong: ${e}`);
Updated 4 days ago