You must create custom middlleware which will be used instead of standart one.
Here is how to do it:
Create file /src/middlewares/request-logger.ts
export default (config, { strapi }) => {
return async (context, next) => {
const start = Date.now();
await next();
const delta = Math.ceil(Date.now() - start);
const ip =
context.request.ip ||
context.request.headers["x-forwarded-for"] ||
context.request.headers["x-real-ip"] ||
context.ip ||
"unknown";
strapi.log.http(`${ip} | ${context.status} ${delta.toString().padStart(3)}ms ${context.method} ${context.url}`);
if (context.status < 400) {
return;
}
strapi.log.error("============================================");
if (context.status >= 500) {
strapi.log.error("INTERNAL SERVER ERROR DETAILS");
}
if (context.status >= 400 && context.status < 500) {
strapi.log.error("CLIENT ERROR DETAILS");
}
strapi.log.error("============================================");
strapi.log.error(`Status code: ${context.status}`);
strapi.log.error(`Message: ${context.response.message}`);
strapi.log.error(`IP Address: ${ip}`);
strapi.log.error(`Method: ${context.method}`);
strapi.log.error(`URL: ${context.url}`);
strapi.log.error(`User-Agent: ${context.request.headers["user-agent"] || "N/A"}`);
strapi.log.error(`Context: ${JSON.stringify(context.request, null, 2)}`);
if (context.request.body && Object.keys(context.request.body).length > 0) {
strapi.log.error(`Request Body: ${JSON.stringify(context.request.body, null, 2)}`);
}
if (context.request.query && Object.keys(context.request.query).length > 0) {
strapi.log.error(`Request Query: ${JSON.stringify(context.request.query, null, 2)}`);
}
strapi.log.error("============================================");
};
};
and register it in /config/middlewares.ts
export default [
// "strapi::logger", // We will disable standart one
"global::request-logger", // And register our custom middleware
// Here existing middlewares ...
];
I have used Nginx as reverse proxy, so I have to scan headers for real request IP address
Also, if you are using Nginx, you have to configure Nginx to proxy headers.
Configure koa-proxy in /config/server.ts:
export default ({ env }) => ({
host: env("HOST", "0.0.0.0"),
port: env.int("PORT", 1337),
url: env("URL", "http://localhost:1337"),
app: {
keys: env.array("APP_KEYS"),
},
proxy: {
koa: true,
},
});
Here is nginx configuration /etc/nginx/sites-available/example.com:
upstream strapi {
server 127.0.0.1:1337;
}
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/ssl/abc/example.com.pem;
ssl_certificate_key /etc/ssl/abc/privatekey.key;
location / {
proxy_pass http://strapi;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass_request_headers on;
include proxy_params;
}
}