How to Create API to View Logs in Node.js ? - GeeksforGeeks (2024)

Last Updated : 21 Jun, 2024

Summarize

Comments

Improve

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.

Table of Content

  • Using Winston logging Framework
  • Using Log4js logging framework

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.jsconst express = require('express');const winston = require('winston');const app = express();// Create a Winston logger with transports// for logging to console and fileconst 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 logsapp.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 readyapp.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:

How to Create API to View Logs in Node.js ? - GeeksforGeeks (1)

Routing the “/logs” Path on the browser:

How to Create API to View Logs in Node.js ? - GeeksforGeeks (2)

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

How to Create API to View Logs in Node.js ? - GeeksforGeeks (3)

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.jsconst express = require("express");const app = express();const log4js = require("log4js");const fs = require("fs");// Configure log4js to write logs to a filelog4js.configure({ appenders: {  file: {  type: "file",  filename: "logs.log"  }  }, categories: { default: { appenders: ["file"], level: "info" } },});// Create a logger objectconst logger = log4js.getLogger();// Create a route to view logsapp.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 statementslogger.info("Info message");logger.warn("Warning message");logger.error("Error message");// Start the serverapp.listen(3000, () => { console.log("Server started on port 3000");});

Run the index.js file using the below command:

node index.js 

Output:

How to Create API to View Logs in Node.js ? - GeeksforGeeks (4)

The output is also saved in logs.log file:

How to Create API to View Logs in Node.js ? - GeeksforGeeks (5)



I

ianurag

How to Create API to View Logs in Node.js ? - GeeksforGeeks (6)

Improve

Next Article

How to Create Modules in Node.js ?

Please Login to comment...

How to Create API to View Logs in Node.js ? - GeeksforGeeks (2024)
Top Articles
In Norway, the Official Fourth Meal Is Breakfast at Night
Is There a High Demand For Life Coaches? - Life Purpose Institute
How To Fix Epson Printer Error Code 0x9e
Asist Liberty
Terrorist Usually Avoid Tourist Locations
El Paso Pet Craigslist
Paris 2024: Kellie Harrington has 'no more mountains' as double Olympic champion retires
Explore Tarot: Your Ultimate Tarot Cheat Sheet for Beginners
Kobold Beast Tribe Guide and Rewards
The Potter Enterprise from Coudersport, Pennsylvania
Aiken County government, school officials promote penny tax in North Augusta
Waive Upgrade Fee
Carter Joseph Hopf
2013 Chevy Cruze Coolant Hose Diagram
Urban Dictionary Fov
1Win - инновационное онлайн-казино и букмекерская контора
Craigslist Pets Sac
2021 Lexus IS for sale - Richardson, TX - craigslist
Craiglist Galveston
Magic Mike's Last Dance Showtimes Near Marcus Cedar Creek Cinema
Dr Adj Redist Cadv Prin Amex Charge
Soccer Zone Discount Code
Vipleaguenba
MyCase Pricing | Start Your 10-Day Free Trial Today
2487872771
What Individuals Need to Know When Raising Money for a Charitable Cause
Soul Eater Resonance Wavelength Tier List
January 8 Jesus Calling
Preggophili
O'reilly's In Monroe Georgia
Lcsc Skyward
Paradise Point Animal Hospital With Veterinarians On-The-Go
Srjc.book Store
Revelry Room Seattle
Amazing Lash Bay Colony
Isablove
FREE Houses! All You Have to Do Is Move Them. - CIRCA Old Houses
Ket2 Schedule
Bismarck Mandan Mugshots
Gets Less Antsy Crossword Clue
8 Ball Pool Unblocked Cool Math Games
Gt500 Forums
Immobiliare di Felice| Appartamento | Appartamento in vendita Porto San
Directions To Cvs Pharmacy
8776725837
Poe Self Chill
Strange World Showtimes Near Century Stadium 25 And Xd
Csgold Uva
3367164101
Kushfly Promo Code
Bbwcumdreams
Latest Posts
Article information

Author: Aron Pacocha

Last Updated:

Views: 5683

Rating: 4.8 / 5 (48 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Aron Pacocha

Birthday: 1999-08-12

Address: 3808 Moen Corner, Gorczanyport, FL 67364-2074

Phone: +393457723392

Job: Retail Consultant

Hobby: Jewelry making, Cooking, Gaming, Reading, Juggling, Cabaret, Origami

Introduction: My name is Aron Pacocha, I am a happy, tasty, innocent, proud, talented, courageous, magnificent person who loves writing and wants to share my knowledge and understanding with you.