Era Software

Writing data from Node.js

Estimated reading time: 3 minutes

This page shows how to write Node.js application logs into EraSearch. In this guide, you'll:

  • Create a sample Node.js application with Winston as a logging framework
  • Write data to EraSearch using the Elasticsearch transport
  • View the logs in the EraSearch UI

Before you begin

This content is intended for engineers and developers using EraSearch on EraCloud. To create an EraCloud account, visit the Getting started series. You'll need your EraSearch Service URI and API key to complete the steps below.

This page also assumes you've installed Node.js.

Instructions

Step 1: Create a sample Node.js application

  1. In your terminal, enter this command to initialize a node project in a new sample directory:

    $ mkdir sample && cd sample && npm init -y
    

    The command returns this output:

    Wrote to .../sample/package.json:
    
    {
      "name": "sample",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "keywords": [],
      "author": "",
      "license": "ISC"
    }
    
  2. Create a small server by adding this code to a new file named app.js:

    require("http")
      .createServer((_, res) => {
        res.end("Hello World");
      })
      .listen(3000);
    
  3. Start the server:

    $ node app.js
    
  4. To verify your setup, go to a separate terminal window and enter the command below. If the server is up and running, the command returns Hello World.

    $ curl localhost:3000
    

To stop the server, press Ctrl + C.

Step 2: Configure Winston

  1. In the sample directory, add Winston to your node project:

    $ npm install winston
    
  2. To create a Winston logger and emit your first log message, update app.js to look like the following:

    const logger = require("winston").createLogger();
    
    require("http")
      .createServer((_, res) => {
        logger.info({
          app: "sample",
          message: "Message received",
        });
        res.end("Hello World");
      })
      .listen(3000);
    
  3. To verify your configuration, restart the server and make the same $ curl localhost:3000 request.

    The server's log output shows that it received a document but doesn't have a logging backend. You'll set up the backend in step 3.

    [winston] Attempt to write logs with no transports {"message":"Request received","level":"info"}
    

Step 3: Configure an EraSearch transport

  1. In your sample directory, add the winston-elasticsearch transport to your node project:

    $ npm install winston-elasticsearch
    
  2. Configure the transport and add it to Winston. Add the following to the top of app.js, replacing YOUR_SERVICE_URI and YOUR_API_KEY with your EraCloud account information:

    const {
      ElasticsearchTransport,
      ElasticsearchTransformer,
    } = require("winston-elasticsearch");
    
    const esTransport = new ElasticsearchTransport({
      buffering: false,
      transformer: (logData) => {
        const transformed = ElasticsearchTransformer(logData);
        delete transformed["@timestamp"];
        transformed._ts = Date.now();
        return transformed;
      },
      clientOpts: {
        node: "YOUR_SERVICE_URI",
        auth: {
          bearer: "YOUR_API_KEY",
        },
      },
    });
    
  3. Next, in app.js, update the existing logger declaration to tell Winston to use the new transport:

    const logger = require("winston").createLogger({
    transports: [esTransport],
    });
    

Note: The configuration above uses the Elasticsearch transport. That workflow is possible because EraSearch suppports much of the Elasticsearch API.

Here's some more information about the configuration above:

  • Buffering is disabled (buffering: false) because EraSearch doesn't support it yet
  • delete transformed["@timestamp"] replaces Elasticsearch's timestamp field (@timestamp) with EraSearch's timestamp field (_ts)

Step 4: View your data in the EraSearch UI

  1. Restart the server:

    $ node app.js
    
  2. In a separate terminal window, send your server a request:

    $ curl localhost:3000
    
  3. To access the EraSearch UI, go to your EraCloud account and click EraSearch UI. Your logs are in the index called logs-YYYY.MM.DD.

Next steps

You can extend your existing configuration to do the following:

  • Add custom fields to your log messages, for example, logger.info({message: "abc123", customField: "value1"})
  • Configure a static index name by adding index: "myIndex" alongside buffering in your transport options

To learn more about writing data to EraSearch, visit these guides: