Subburaj
Subburaj

Reputation: 5192

streaming nodejs application logs to sumolog

I have my application running in EC2 machine, need to stream all the logs of my application to sumologs..

I did a POC for this as below and its working fine

var sumoURL = "https://xyz"
// Sample log message
const logMessage = {
    timestamp: new Date().toISOString(),
    level: 'error',
    message: 'This is a sample error message.',
    // You can add additional fields as needed
  };
   
  // Convert log message to JSON string
  const logData = JSON.stringify(logMessage);
   
  // HTTP request options
  const options = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Content-Length': logData.length,
    },
  };
   
  // Create HTTP request
  const req = https.request(sumoURL, options, (res) => {
    console.log(`Status: ${res.statusCode}`);
    console.log(`Headers: ${JSON.stringify(res.headers)}`);
    res.setEncoding('utf8');
    res.on('data', (chunk) => {
      console.log(`Body: ${chunk}`);
    });
  });
   
  // Handle HTTP request errors
  req.on('error', (error) => {
    console.error(`Error: ${error.message}`);
  });
   
  // Send log message
  req.write(logData);
  req.end();

I can see this logs in sumo.

But in the above approach we looks like we need to make http request every time for each logs. Is there any other optimized way to achieve this?

Also its possible to use log4js to stream logs to sumolog?

Upvotes: 0

Views: 49

Answers (1)

Grzegorz Oledzki
Grzegorz Oledzki

Reputation: 24281

I think you have four options:

  1. Sumo Logic accepts log data by simple HTTP POST requests. The feature on the Sumo Logic side is called HTTP Source in a hosted collector.
  2. Install a Sumo Logic OpenTelemetry Collector to run on your EC2 Machine and let it "forward" all the local log files to the Sumo Logic product.
  3. Similarly, one can install Sumo-specific Installed collector for the same purpose
  4. Another option I can see is using https://github.com/johncblandii/log4js-sumologic-appender which is a community project. I don't have any experience with it.

I've listed the options starting from the easiest.

Disclaimer: I am currently employed by Sumo Logic.

Upvotes: 0

Related Questions