Logging

Handling logs efficiently and effectively is crucial for maintaining, debugging, and monitoring an application. Velocity utilizes the power of pino, a fast, low-overhead Node.js logging library, for logging and Sentry, a real-time error tracking tool, for monitoring and fixing crashes in real-time.

Configuration

Logging is managed based on the configured log level in the config.ts file of the shop package. This configuration determines the minimal level of severity for messages that will be logged. The severity levels, in ascending order, are: 'trace', 'debug', 'info', 'warn', 'error', and 'fatal'.

Messages of severity 'warn', 'error', or 'fatal' are not only logged but are also automatically reported to Sentry, providing you with a clear picture of the application's health and potential areas of concern.

Request Logging

Besides logging messages or errors, Velocity also enriches logs with relevant metadata about the current request, including the request id. This inclusion of request-specific data in the logs allows for grouping all messages related to a specific request, tracking issues across multiple request-response cycles, and providing crucial context when investigating an issue.

The log utility function, located in src/utilities/logging.ts, facilitates this functionality.

Usage

You can invoke the log function whenever you need to record an event in your application. This function accepts a severity level, an error or message to log, and optional extra data that can be useful for context.

Here's an example of logging a warning message:

log('warn', 'This is a warning');

To log errors:

log('error', new Error('test'));

In these examples, the first argument represents the severity level of the message, and the second argument is the message or error itself.

Understanding the log Utility

Under the hood, the log utility function retrieves the current request using the getRequest helper, then logs the message or error at the appropriate level using pino's logging methods (request.log[level]).

If the level is 'fatal', 'error', or 'warn', the function also reports the event to Sentry using Sentry.captureException. Sentry reports contain not only the error or message but also the severity level, extra data, and user-specific information (such as the IP address, customer access token, and cart ID).

By leveraging this log function, you ensure that all important events and potential issues in your application are properly recorded and monitored, providing valuable insights and accelerating the debugging process.

Powered by Doctave