![]() |
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 Introduction to Log ManagementLogs 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:
Table of Content Using Winston logging FrameworkSteps to create API to view logs in Node.js using WinstonStep 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'); Step 3: You can use Winston to log more detailed information. const logger = winston.createLogger({ 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) => { Step 6: Start the server: Finally, start the Express app by listening on a port: const port = process.env.PORT || 3000; 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.
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 frameworkIn 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.
Run the index.js file using the below command: node index.js Output: ![]() The output is also saved in logs.log file: ![]() |
Reffered: https://www.geeksforgeeks.org
Node.js |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |