Docs
Core Concepts
Logging and Monitoring

Logging and Monitoring

Learn how Pylon enhances visibility into your application's performance and behavior with its integrated logging and monitoring capabilities, ensuring smooth operations and rapid issue resolution.

Overview

Pylon integrates robust logging and error monitoring capabilities to help you maintain and monitor your web services effectively. In this section, we'll explore how to use the built-in logging features powered by the Winston logging library and how to set up error monitoring with Sentry.

Logging with Winston

Pylon's logging functionality leverages the powerful and flexible Winston library, allowing you to capture and manage logs efficiently. Here's a basic example of how to define a service with logging:

import { defineService, logger } from "@getcronit/pylon";
 
export default defineService({
  Query: {
    sum: (a: number, b: number) => {
      logger.info(`Calculating sum of ${a} and ${b}`);
      return a + b;
    },
  },
  Mutation: {
    divide: (a: number, b: number) => {
      if (b === 0) {
        logger.error('Attempt to divide by zero');
        throw new Error('Division by zero is not allowed');
      }
      logger.info(`Dividing ${a} by ${b}`);
      return a / b;
    },
  },
});

Using Winston Log Levels

Winston provides various log levels to categorize the importance and type of messages:

  • error: for error messages
  • warn: for warning messages
  • info: for informational messages
  • http: for HTTP requests
  • verbose: for verbose logging
  • debug: for debugging messages
  • silly: for highly detailed messages

You can use these log levels to capture different types of logs as needed. For more information on Winston, refer to the Winston documentation (opens in a new tab).

Error Monitoring with Sentry

Pylon supports automatic integration with Sentry, a popular error tracking and monitoring service. To enable this integration, you need to set the SENTRY_DSN environment variable. Once this is configured, all logs and errors are automatically sent to your Sentry project.

Setting Up Sentry

  1. Get your Sentry DSN: Sign up for a Sentry account and create a new project. Obtain the DSN (Data Source Name) for your project.

  2. Set the SENTRY_DSN Environment Variable: Configure the SENTRY_DSN in your environment. This can typically be done in your project's environment configuration file or through your deployment pipeline.

SENTRY_DSN=https://[email protected]/project-id
  1. Deploy your Pylon service: With the SENTRY_DSN environment variable set, deploy your Pylon service. Logs and errors will now be automatically sent to Sentry.

Example

import { defineService, logger } from "@getcronit/pylon";
 
export default defineService({
  Query: {
    sum: (a: number, b: number) => {
      logger.info(`Calculating sum of ${a} and ${b}`);
      return a + b;
    },
  },
  Mutation: {
    divide: (a: number, b: number) => {
      if (b === 0) {
        logger.error('Attempt to divide by zero');
        throw new Error('Division by zero is not allowed');
      }
      logger.info(`Dividing ${a} by ${b}`);
      return a / b;
    },
  },
});

In this example, any errors or logs generated by the sum and divide operations will be captured by Winston and sent to Sentry if the SENTRY_DSN is configured.

Conclusion

By integrating Winston for logging and Sentry for error monitoring, Pylon provides a comprehensive solution to track and manage the health and performance of your web services. Ensure that your SENTRY_DSN is set up correctly to take full advantage of these powerful tools, enabling you to maintain a robust and reliable application.

For more details, refer to the official documentation: