New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

api-request-logger

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

api-request-logger

Minimilistic middleware to log every api call to the server

latest
Source
npmnpm
Version
1.2.0
Version published
Maintainers
1
Created
Source

api-request-logger

maintainer: Rishu Shrivastav (rish15)

Introduction

The why ?

So we wanted to track every request that comes to the server with some metadata such as userAgent, reqBody, apiPath, apiLatency so I put together a simple minimilistic middleware which can be injected in one liner and start giving data to be analysed. Adding to that it also provides a wrapper over winston library for logging, helping in generting formatted logs and requestId associated with the log statemenet.

Getting started

1. Installation

npm i api-request-logger

Usage

const { apiLogger } = require("api-request-logger");`
app.use(apiLogger)` add this line before the routes in the entry file

Logging format

{
  "requestUrl": "http://localhost:2000/serviceName/v1/users/list",
  "requestMethod": "GET",
  "userAgent": "PostmanRuntime/7.28.4",
  "xRequestId": "f584d507e38a9f3a2e88b02e7ab7e81b",
  "apiLatency": 51.71452800184488,
  "time": "2023-06-09T05:54:45.901Z",
  "reqBody": {},
  "env": "local",
  "msg": "api_stats"
}

Logging

the log is just a wrapper over the Winston library, it formats the logs in [level] [timestamp] [requestId] [logs] it appends the requestId to every log statement.

Import the module

const { log } = require("api-request-logger");

Example

log.info("This is an info");
log.warn("This is a warning");
log.error("This is an error");
changing log level

For detailed log level priorities refer this winston log levels

In the .env file or any configuration file that your app uses, export an env variable LOG_LEVEL=level where the level is the log levels supported by Winston. By default log level is set to info

LOG_LEVEL=debug

Keywords

api

FAQs

Package last updated on 20 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts