VTEX IO provides a logging service that allows developers to keep track of errors, warnings, and informative events within an application. 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 logs.
The VTEX IO Logging Service is currently available for Node apps only.
Before you begin
To complete this guide, you must have the VTEX IO CLI installed in your machine. For more information, please refer to this document.
Step by step
Implementing the VTEX IO Logging Service
- Open your Node app in any code editor of your preference.
- Define an object with the
Context
interface as a parameter of a function you wish to provide logging messages. Take 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, which is an implementation insidevtex
responsible for generating log messages.
- Destructure the
logger
object from thevtex
context and use its methods (i.e.,error
,warn
,info
, anddebug
) to provide error, warning, debugging, or informative messages in the log. Take 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 inside a VTEX IO service application is intercepted and automatically logged with a
logger.error
implementation.
Retrieving application logs
Every log written by a running application with the VTEX IO logging service is collected and stored for 7 days. These logs can be retrieved by the VTEX IO CLI as in the following:
- 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
As an output, you should see 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}
. Remeber to 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 the most suitable name for you.
Also, if you want to see log messages that you have previously retrieved, run vtex logs --past
.