Open In App

How to create API to view logs in Node.js ?

Last Updated : 16 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

API stands for Application Programming Interface. An API is a set of protocols, routines, and tools that are used to build software applications. APIs provide a way for different software applications to communicate with each other, exchange data, and share functionality.

In other words, APIs define the way that different software components can interact with each other. APIs specify the inputs and outputs of software components, as well as the methods for accessing them. APIs can be used to access web services, databases, operating systems, hardware devices, and other software components.

In this article, we will discuss the different approaches to creating an API to view logs in Node.js. The logs are helpful to understand the behavior of the web service that is containing thousands of APIs, and microservices.

 

Approach 1: 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.

Javascript




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.

Javascript




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:

Javascript




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.

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. 

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.  

Index.js

Javascript




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');
});


Run the index.js file using the below command:

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:

 

Approach 2: Using Log4js logging framework:

Example: 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.

Index.js

Javascript




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:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads