After recently deploying a Node JS API to Azure, I needed to view log messages from the deployed instance because part of it wasn’t working as expected. In a Node JS API in Azure, log messaging is not switched on by default. In this post, I’ll explain how to enable logging in Azure and view the log messages.
Enable log messages in Azure
To enable log messages in an Azure Linux app :
- Go to the Azure Portal and log in
- Navigate to your app from the App Services list
- Click on App Service Logs which is in the Monitoring section
- Select File System
- In Quote MB specify the quota. I left mine as the default value.
- In Retention Period set the number of days you want the logs to be kept for
- Click Save
Write log messages in Node JS code
To create a log message that you will be able to view in Azure simply use console.log
, like this:
app.get("/product", (req, res) => { const name = req.query.name; // A basic log message console.log("This is a log message"); // A log message wrapped in back ticks so that a // variable can be included console.log(`The query param name: ${name} `); res.send({ name: name, }); });
View the Log messages in Azure
To view the log messages in Azure from the Node JS app follow these steps :
- Go to the Azure portal
- Navigate to your app
- Click on Log Stream in the Monitoring section
A window will open and you’ll see your log messages appear in this window as the application runs. When you’re running your API locally in VS Code you’d be seeing these messages in either your terminal window or the debug console.
I’ve also written a post about using Log messages in Azure from a .Net Core API.