App logs and troubleshooting
Use the Optimizely Connect Platform (OCP) App SDK's logger to assist in the development and debugging of your application.
To assist in development and debugging, apps can log messages using the Optimizely Connect Platform (OCP) App SDK's logger. Log access differs by role, as described below:
- Optimizely Support – Can view all logs for all OCP applications.
- App developers – Can view logs produced by their app in the context of Optimizely Data Platform (ODP) accounts they have access to (typically a sandbox account). This includes logs from lifecycle hooks, functions, and jobs, but not global functions. In the OCP command-line interface (CLI), run the
ocp app logs
command to access your logs. - ODP customers – Can view logs for apps they have installed to their ODP account. ODP customers access these logs in the Troubleshooting tab of the app in the ODP App Directory.
Note
Logs are intended for debugging and troubleshooting purposes. To provide customer-facing messages in the ODP Activity Log, use Activity Log notifications.
Log levels
You can log messages with one of the following log levels:
info
debug
– The default log level for-dev
tagged app versions.warn
– The default log level for full release andbeta
pre-release app versions.error
To check the current log level for an app installation, run the following command in the OCP CLI:
ocp app get-log-level <APP-VERSION> --trackerId=<TRACKER-ID>
To change the log level for an app installation:
ocp app set-log-level <APP-VERSION> <NEW-LOG-LEVEL> --trackerId=<TRACKER-ID>
Note
The change is valid for two hours and then the log level is reset to the default value.
Use the logger
in place of the console
to ensure that you capture all messages properly. However, as a safety measure for uncontrolled usages in dependencies and more, the app runtime captures console
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.
Import OCP App SDK logger
logger
The OCP App SDK exposes the logging feature through the logger
instance, which you can import as follows:
import {logger} from '@zaiusinc/app-sdk';
The logger
instance follows the SDK's ILogger
interface:
/**
* OCP Logger 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;
}
Capture stack traces
In order to capture a stack trace, you must supply the error 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}`);
Access logs in the OCP CLI
As a developer, you can use the OCP CLI to access logs produced by your app in the context of ODP accounts you have access to (typically a sandbox account).
To access your app logs:
ocp app logs --appId=<APP-ID> --trackerId=<TRACKER-ID>
By default, the command returns logs from last 24 hours. You can specify a different time range using the --from
and --to
options. As with most OCP CLI commands, use the -a/--availability
option to query logs from regions other than the default US region.
To query logs produced by a job execution:
ocp app logs --jobId=<JOB-ID>
Note
Use the
ocp job list
command to get the job ID.
To fetch build logs (produced by the ocp app prepare
command):
ocp app logs --buildId=<BUILD-ID>
Note
Use the
ocp app prepare
command to get the build ID.
To view more details on all other logging options, add the help context (-h
) to the ocp app logs
command:
$ ocp app logs -h
Active environment: production
@optimizely/ocp-cli 1.0.3 (Apache-2.0)
Usage: ocp app logs [--tail] [--appId=<appId>] [--buildId=<buildId>] [--appVersion=<appVersion>] [--trackerId=<trackerId>] [--jobId=<jobId>] [--level=<level>] [--from=<from>] [--to=<to>]
[--search=<search>] [--availability=<availability>]
Flags:
--tail Tails the logs
Options:
--appId The App ID (e.g. my_app). Either App ID or Build ID must be specified.
--buildId The ID of the build. Either App ID or Build ID must be specified. Build IDs are printed by 'ocp app prepare' command.
--appVersion The app version (e.g. 1.0.0)
--trackerId The Tracker ID of the ODP account (required for developers)
--jobId Show logs for given execution of a job. Use 'ocp jobs list' command to get the job ID
--level Filter app logs by log level. Valid values: INFO, DEBUG, WARN, ERROR
--from A start time as an ISO string, an epoch timestamp, or relative string (i.e. "5m" for 5 minutes.) Default: 24h
--to A end time as an ISO string, an epoch timestamp, or relative string (i.e. "5m" for 5 minutes)
--search String to search for in log messages. Can be specified multiple times.
--availability, -a The availability zone that will be targeted (default: us)
Access logs in the ODP App Directory
ODP customers can access logs in the Troubleshooting tab of the apps they have installed in the ODP App Directory. This page provides similar functionality as the ocp app logs
command. ODP customers can use it to troubleshoot issues with integrations, possibly in collaboration with the app developer.
Updated 12 months ago