![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
strapi-plugin-request-id
Advanced tools
Add a unique id to each request made to your server and track your users' activity in the logs.
Add a unique id to each request made to your server and track your users' activity in the logs.
request id
and correlation id
to each request.request id
and correlation id
when a message is logged.Strapi v4 is required.
yarn add strapi-plugin-request-id
or
npm i strapi-plugin-request-id
In config/plugins.js
, add:
module.exports = ({ env }) => ({
//...
"request-id": {
enabled: true,
},
//...
});
request-id
middleware:In config/middlewares.js
, add the middleware at the bottom of the list:
module.exports = [
//...
"plugin::request-id.request-id",
];
When a request is received, a unique request id
and correlation id
are added to it.
☝️ If the header of the request contains the property
X-Correlation-Id
, this value is used as thecorrelation id
instead generating a new one.
The request id
and correlation id
are automatically logged when a message is logged using the strapi logger.
This code:
const endpoint = (ctx) => {
const { user } = ctx.state;
strapi.log.info(`user ${user.id} was there!`);
ctx.status = 200;
};
Will produce the logs:
{
"level": "info",
"message": "user 1 was there!",
"timestamp": "2022-05-29 17:26:19",
"x-correlation-id": "e5b97199-9003-4c36-8ead-28186e3f4a4f",
"x-request-id": "04dd66c9-3d3e-4188-8ae8-32703d864862"
}
You can then track user 1
's activity by filtering the logs with x-request-id="04dd66c9-3d3e-4188-8ae8-32703d864862"
, or accross your services using x-correlation-id
.
By default, strapi doesn't display the logs as json in the console. If you want to see the request id
and correlation id
while developing, create the file config/logger.js
and use this configuration:
"use strict";
const { winston } = require("@strapi/logger");
module.exports = {
transports: [
new winston.transports.Console({
level: "silly",
format: winston.format.combine(
winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
winston.format.json()
),
}),
],
};
The request id
and correlation id
are added to the headers of the response. They can be accessed through the ctx.response
object:
const endpoint = (ctx) => {
const requestId = ctx.response.get("X-Request-Id");
const correlationId = ctx.response.get("X-Correlation-Id");
const res = await fetch(`my-other-service/endpoint`, {
method: "GET",
headers: {
// Add the correlation id when calling an external service.
"X-Correlation-Id": correlationId
},
});
ctx.body = { data: res.data };
};
Baboo - @Baboo7
FAQs
Add a unique id to each request made to your server and track your users' activity in the logs.
The npm package strapi-plugin-request-id receives a total of 159 weekly downloads. As such, strapi-plugin-request-id popularity was classified as not popular.
We found that strapi-plugin-request-id demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.