How to Create API to View Logs in Node.js ?
Last Updated :
21 Jun, 2024
Log files are essential for monitoring and troubleshooting applications. They provide insight into the application’s behaviour and help identify issues. In a Node.js application, you might want to create an API that allows you to view these logs easily. This can be particularly useful for centralized log management, debugging, and remote access to log data.
This article will guide you through the process of creating a simple API to view logs in a Node.js application. We will use the express
framework to build our API and a logging library to manage the log files.
Introduction to Log Management
Logs are records of events that happen in an application. They help developers understand what’s going on under the hood and are invaluable for diagnosing issues. Common practices for managing logs include:
- Centralized logging: Collecting logs from different sources in one place.
- Log rotation: Managing the size and number of log files.
- Access control: Restricting who can view or manipulate logs.
Using Winston logging Framework
Steps to create API to view logs in Node.js using Winston
Step 1: Install the required dependencies, You will need to install some packages like express, morgan, cors, and winston to create the API. You can install them using npm by running the following command in your terminal
npm install express winston
Step 2: Create an Express app, Create a new file app.js and import the necessary modules
const express = require('express');
const winston = require('winston');
const app = express();
Step 3: Â You can use Winston to log more detailed information.
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'your-service-name' },
transports: [
new winston.transports.File({ filename:
'error.log', level: 'error' }),
new winston.transports.File({
filename: 'combined.log'
})
]
});
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple()
}));
}
Step 4: Define the API endpoint: You can define a route to retrieve the logs and return them as a JSON response.
app.get('/logs', (req, res) => {
logger.query({ order: 'desc', limit: 100 },
(err, result) => {
if (err) {
res.status(500).send({
error: 'Error retrieving logs'
});
} else {
res.send(result);
}
});
});
 Step 6: Start the server: Finally, start the Express app by listening on a port:
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
This API will retrieve the latest 100 logs and return them as a JSON response when the /logs endpoint is accessed. You can customize the logger to log more detailed information or write the logs to different files or databases based on your requirements.
We set the logger's log level to 'info' and format the log entries in JSON format. We created a route path "/logs" that query the latest last 100 log entries using logger.query(). We pass in an object with options for the query - order is set to 'desc' to retrieve the latest entries first, and the limit is set to 100 to retrieve the latest 100 entries. Â
Example: In this example, we create a Winston logger instance with winston.createLogger(). The logger has two types of transport - one for logging into the console and one for logging into a file named app.log.Â
Node
// index.js
const express = require('express');
const winston = require('winston');
const app = express();
// Create a Winston logger with transports
// for logging to console and file
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.Console(),
new winston.transports.File({
filename: 'app.log'
})
]
});
// Define an API route for viewing the logs
app.get('/logs', (req, res) => {
// Query the logger for the latest log entries
logger.query({ order: 'desc', limit: 100 },
(err, results) => {
if (err) {
// If an error occurs, send an
// error response
res.status(500).send({
error: 'Error retrieving logs'
});
} else {
// If successful, send the log
// entries as a response
res.send(results);
}
});
});
// Start the server and log a message when
// it's ready
app.listen(3000, () => {
logger.info('Server started on port 3000');
});
Step to Run Application:Â Run the application using the following command from the root directory of the project
node index.js
Output:

 Routing the "/logs" Path on the browser:

 The output is in JSON format. The output is also saved in a file called app.log:

Using Log4js logging framework
In this code, we first import the necessary packages: Express and Log4js. We then configure Log4js to write logs to a file called logs.log using the file appender. We create a logger object that we can use to write logs to. We then create an endpoint for our API to view logs, which reads logs from the file and returns them as a JSON response.
Example: Implementation to create API to view logs in Node.js using above method.
Node
// index.js
const express = require("express");
const app = express();
const log4js = require("log4js");
const fs = require("fs");
// Configure log4js to write logs to a file
log4js.configure({
appenders: {
file: {
type: "file",
filename: "logs.log"
}
},
categories: {
default: {
appenders:
["file"], level: "info"
}
},
});
// Create a logger object
const logger = log4js.getLogger();
// Create a route to view logs
app.get("/logs", (req, res) => {
// Read logs from file
const logs = fs.readFileSync("logs.log", "utf8");
// Return logs as a JSON response
res.json({ logs: logs });
});
// Example logging statements
logger.info("Info message");
logger.warn("Warning message");
logger.error("Error message");
// Start the server
app.listen(3000, () => {
console.log("Server started on port 3000");
});
Run the index.js file using the below command:
node index.js
Output:

The output is also saved in logs.log file:
Similar Reads
How to Setup View Engine in Node.js ?
View engines are useful for rendering web pages. There are many view engines available in the market like Mustache, Handlebars, EJS, etc but the most popular among them is EJS which simply stands for Embedded JavaScript. It is a simple templating language/engine that lets its user generate HTML with
2 min read
How to Create and Verify JWTs with Node?
In this article, we will see how to create JWT tokens in Node.js. We will implement secure authentication in Node.js by creating and verifying JSON Web Tokens (JWTs) using libraries like `jsonwebtoken`. Prerequisites:Good knowledge of JavaScript.Basic knowledge about Express JS.Basic knowledge about
5 min read
How to Connect Node.js to Woocommerce API ?
WooCommerce is one of the most popular e-commerce platforms available, powering over 30% of all online stores. It is built on top of WordPress and provides a powerful API for developers to interact with their store data programmatically. If you are building a Node.js application that interacts with
4 min read
How to Use ChatGPT API in NodeJS?
ChatGPT is a very powerful chatbot by OpenAI that uses Natural Language Processing to interact like humans. It has become very popular among developers and is being widely used for some of its state-of-the-art features, like understanding and interpreting code, and even generating code based on text
4 min read
How to Add Data in JSON File using Node.js ?
JSON stands for Javascript Object Notation. It is one of the easiest ways to exchange information between applications and is generally used by websites/APIs to communicate. For getting started with Node.js, refer this article. Prerequisites:NPM NodeApproachTo add data in JSON file using the node js
4 min read
How to Create a MySQL REST API
Creating a REST API is important for enabling communication between different software systems. MySQL is one of the most popular relational database management systems which serves as the backbone for data storage in web applications. In this article, we will learn how to create a REST API using MyS
6 min read
Using Restify to Create a Simple API in Node.js
Restify is an npm package that is used to create efficient and scalable RESTful APIs in Nodejs. The process of creating APIs using Restify is super simple. Building a RESTful API is a common requirement for many web applications. Restify is a popular Node.js framework that makes it easy to create RE
6 min read
How to Create A REST API With JSON Server ?
Setting up a RESTful API using JSON Server, a lightweight and easy-to-use tool for quickly prototyping and mocking APIs. JSON Server allows you to create a fully functional REST API with CRUD operations (Create, Read, Update, Delete) using a simple JSON file as a data source. Table of Content GET Re
4 min read
How to Access Cache Data in Node.js ?
Caching is an essential technique in software development that helps improve application performance and scalability by temporarily storing frequently accessed data. In Node.js, caching can significantly reduce database load, minimize API response times, and optimize resource utilization. This artic
5 min read
How to add new functionalities to a module in Node.js ?
Node.js is an open-source and cross-platform runtime environment built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. You need to recollect that NodeJS isnât a framework, and itâs not a programming language. In this article, we will discuss how to add new functi
3 min read