Implementing real-time logging with Azure Service Bus in Node.js

In this blog post, we will discuss how to implement real-time logging using the Azure Service Bus and Node.js. Logging is a critical component of any application, as it allows us to track and monitor the behavior and performance of our system. By using Azure Service Bus, we can easily send log messages asynchronously to our logging infrastructure, ensuring that our application remains highly responsive.

Prerequisites

Before we begin, ensure that you have the following prerequisites in place:

Setting up Azure Service Bus

  1. Create a new Azure Service Bus namespace in the Azure portal.
  2. Create a new queue or topic within the namespace to receive log messages.

Installing required dependencies

To get started with our Node.js application, we need to install the @azure/service-bus package using the following command:

npm install @azure/service-bus

Sending log messages to Azure Service Bus

First, we need to initialize our Service Bus client with the connection string and the name of the queue or topic we created earlier.

const { ServiceBusClient } = require("@azure/service-bus");

const connectionString = "<connection-string>";
const queueName = "<queue-name>";

const serviceBusClient = new ServiceBusClient(connectionString);
const sender = serviceBusClient.createSender(queueName);

Now, we can use the sender object to send log messages to Azure Service Bus. For example:

async function logMessage(message) {
    try {
        await sender.sendBatch([
            { body: JSON.stringify(message) }
        ]);
        console.log("Log message sent successfully.");
    } catch (error) {
        console.error("Failed to send log message:", error);
    }
}

logMessage({ level: "info", timestamp: new Date(), message: "Sample log message" });

Receiving log messages from Azure Service Bus

To receive log messages from Azure Service Bus, we need to set up a message handler function.

const connectionString = "<connection-string>";
const queueName = "<queue-name>";

const serviceBusClient = new ServiceBusClient(connectionString);
const receiver = serviceBusClient.createReceiver(queueName);

async function handleMessage(message) {
    try {
        const logMessage = JSON.parse(message.body);
        console.log("Received log message:", logMessage);
        await message.complete();
    } catch (error) {
        console.error("Failed to process log message:", error);
        await message.abandon();
    }
}

receiver.subscribe({
    processMessage: handleMessage,
    processError: handleError
});

function handleError(error) {
    console.error("An error occurred while receiving log messages:", error);
}

Conclusion

With the help of Azure Service Bus, we can easily implement real-time logging in our Node.js applications. By using asynchronous message queuing, we ensure that our logging process doesn’t impact the performance and responsiveness of our application. This allows us to efficiently monitor and analyze the behavior of our system.

#azure #nodejs