VTEX IO provides a logging service that allows developers to keep track of app errors, warnings, and informative events. The VTEX IO Logging Service collects data from the cloud infrastructure where VTEX applications run and delivers them via the VTEX IO CLI.
In the following section, you'll learn how to implement the VTEX IO Logging Service in your apps and how to retrieve their respective logs.
The VTEX IO Logging Service is currently available for Node apps only.
Before you begin
To follow this guide, you must have the VTEX IO CLI installed on your machine. For more information, please see this document.
Instructions
Implementing the VTEX IO Logging Service
- Open your Node app in the code editor of your choice.
- Define an object with the
Context
interface as a parameter in a function where you want to provide logging messages. See the following example:
_10const helloWorld = (ctx: Context) => {_10 const { vtex: { logger } } = ctx_10_10 logger.info('Hello World!')_10}
In this example, the helloWorld
function receives an object with a Context interface as a parameter.
The
Context
interface contains many implementations inherent to the VTEX IO platform. One of those implementations is calledvtex
— an object containing all VTEX IO infrastructure related metadata, such asaccount
,workspace
,tenant
,settings
, and some service implementations. In the previous example, we used thelogger
service, an implementation insidevtex
that generates log messages.
- Destructure the
logger
object from thevtex
context and use its methods (error
,warn
,info
, anddebug
) to provide error, warning, debugging, or informative messages in the log. See the following example:
_10const helloWorld = (ctx: Context) => {_10 const { vtex: { logger } } = ctx_10_10 logger.warn('Warning the world!')_10_10 logger.error('Error!!!')_10_10 logger.debug('Verbose debug message!')_10}
Every exception that happens in a VTEX IO service app is intercepted and automatically logged with a
logger.error
implementation.
Retrieving app logs
Every log written by a running app with the VTEX IO Logging Service is collected and stored for later retrieval.
You can retrieve the logs using the VTEX IO CLI by following the steps:
- Log in to your VTEX account.
- Install the Logs plugin for the VTEX IO CLI by running the following command:
_10vtex plugins add logs
- Check if the installation of the Logs plugin was successful by running
vtex logs --help
. - Retrieve logs from all apps installed in your account by running the following command:
_10vtex logs --all
The output should be a message similar to the following:
_14vtex logs --all_1418:15:26.779 - info: Connecting to logs stream for store components_1418:15:26.782 - info: Press CTRL+C to abort_14_1418:15:43.856 - info: Listening to store components logs_14_14 info: { status: 403,_14 code: 'FORBIDDEN',_14 name: 'ForbiddenError',_14 level: 'error',_14 message: 'This username is already registered with another email',_14 stack:_14 'ForbiddenError: This username is already registered with another email[ErrorStack...]_14 routeId: 'login' }
You can also retrieve logs from a specific app installed in your account by running the following command:
vtex logs {serviceAppExample}
. ReplaceserviceAppExample
with the desired app name.
We suggest running vtex logs --all > {mylogfile.logs}
to save the log messages in a local file. Replace {mylogfile.logs}
with a name that fits your environment.
If you want to see log messages that you have previously retrieved, run vtex logs --past
.
Log storage and retention limits
VTEX limits the storage size and retention period of logs to guarantee the stability and performance of the platform. The limits are:
- Logs are stored for 7 days at most.
- Each app can have up to 1 GB of logs stored per account. If an app generates more than 1 GB of logs in one account in 7 days or less, the oldest logs are deleted.
If you need more than 1 GB of logs per week for an app, we recommend retrieving and storing your logs before they reach the storage limit. You can check the size of the logs from the size of the retrieved files in your local environment. See the instructions in the Retrieving app logs section.
VTEX may change the storage limits of app logs without additional notice. Check this documentation for the updated limit values.