All Products
Search
Document Center

Simple Log Service:Collect Node.js logs

Last Updated:Apr 24, 2024

This topic describes how to use Logtail to collect Node.js logs.

Background information

Log4js is a management tool for Node.js logs. You can use Log4js to send Node.js logs to files and specify a custom log format. This facilitates log collection and management. Log4js defines the following log levels in ascending order: TRACE, DEBUG, INFO, WARN, ERROR, and FATAL. The following sample code provides an example of Log4js configuration:

var log4js = require('log4js');
log4js.configure({
  appenders: [
    {   
      type: 'file', // Send logs to a file.
      filename: 'logs/access.log', 
      maxLogSize: 1024,
      backups:3,
      category: 'normal' 
    }   
  ]
});
var logger = log4js.getLogger('normal');
logger.setLevel('INFO');
logger.info("this is a info msg");
logger.error("this is a err msg");

Procedure

Simple Log Service provides configuration wizards to help you create Logtail configurations. The following example describes how to collect Node.js logs in full regex mode.

  1. Log on to the Simple Log Service console.

  2. In the Import Data section, select RegEx - Text Log.

  3. Select the project and Logstore. Then, click Next.

  4. Create a machine group.

    • If a machine group is available, add the machine group to Applied Server Groups.

    • If no machine group is available, perform the following steps to create a machine group. In this example, an Elastic Compute Service (ECS) instance is used.

      1. On the ECS Instances tab, select the ECS instance and click Create.

        For more information, see Install Logtail on ECS instances.

      2. In the Parameter Confirmation dialog box, click OK.

      3. Make sure that the value of the Execution Status parameter is Success. Then, click Complete Installation.

      4. In the Create Machine Group step, enter a machine group name and retain the default settings for other parameters. Then, click Next.

        For more information, see Create an IP address-based machine group.

  5. Confirm that the machine group is displayed in the Applied Server Groups section and click Next.

    Important

    If you apply a machine group immediately after you create the machine group, the heartbeat status of the machine group may be FAIL. This issue occurs because the machine group is not connected to Simple Log Service. To resolve this issue, you can click Automatic Retry. If the issue persists, see What do I do if no heartbeat connections are detected on Logtail?

  6. In the Logtail Configuration step, create a Logtail configuration.

    For more information about Logtail-related parameters, see Logtail configuration.

  7. Preview data, configure indexes, and then click Next. For more information about indexes, see Create indexes.

  8. Click Log Query. You are redirected to the query and analysis page of your Logstore.

    You must wait approximately 1 minute for the indexes to take effect. Then, you can view the collected logs on the Raw Logs tab. For more information, see Query and analyze logs.