refactor(api): simplify logging (#58707)

Co-authored-by: Mrugesh Mohapatra <1884376+raisedadead@users.noreply.github.com>
This commit is contained in:
Oliver Eyton-Williams
2025-02-14 11:06:16 +01:00
committed by GitHub
parent c739c994ed
commit 5b0f491c52
2 changed files with 38 additions and 32 deletions
+1 -20
View File
@@ -1,5 +1,4 @@
import { type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox';
import { isEmpty } from 'lodash';
/**
* Plugin for the health check endpoint.
@@ -15,25 +14,7 @@ export const statusRoute: FastifyPluginCallbackTypebox = (
done
) => {
fastify.get('/status/ping', async (req, _reply) => {
const url = req.url || 'URL not found';
const reqId = req.id || 'REQ_ID not found';
const headers = isEmpty(req.headers) ? 'HEADERS not found' : req.headers;
const ip =
req.headers['x-forwarded-for'] ||
req.headers['x-real-ip'] ||
req.ip ||
'IP not found';
const query = isEmpty(req.query) ? 'QUERY not found' : req.query;
fastify.log
.child({
URL: url,
REQ_ID: reqId,
HEADERS: headers,
IP: ip,
QUERY: query
})
.debug('returning a ping');
fastify.log.child({ req }).debug('returning a ping');
return { msg: 'pong' };
});
+37 -12
View File
@@ -3,8 +3,12 @@
// is not included in the build (it's a dev dependency).
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path="./reset.d.ts" />
import { randomBytes } from 'crypto';
import { FastifyRequest } from 'fastify';
import { isEmpty } from 'lodash';
import { build } from './app';
import {
FREECODECAMP_NODE_ENV,
@@ -13,16 +17,30 @@ import {
PORT
} from './utils/env';
const requestSerializer = (request: FastifyRequest) => ({
method: request.method,
url: request.url,
ip: request.headers['x-forwarded-for'] || request.ip,
hostname: request.hostname,
remoteAddress: Array.isArray(request.headers['x-forwarded-for'])
? request.headers['x-forwarded-for'][0]
: request.headers['x-forwarded-for'] || request.ip,
remotePort: request.socket.remotePort
});
const requestSerializer = (req: FastifyRequest) => {
const method = req.method || 'METHOD not found';
const url = req.url || 'URL not found';
const headers = req.headers || 'HEADERS not found';
const xForwardedFor = Array.isArray(req.headers['x-forwarded-for'])
? req.headers['x-forwarded-for'][0]
: req.headers['x-forwarded-for'];
const ip =
xForwardedFor || req.headers['x-real-ip'] || req.ip || 'IP not found';
const query = isEmpty(req.query) ? 'QUERY not found' : req.query;
const hostname = req.hostname || 'HOSTNAME not found';
const remotePort = req.socket.remotePort || 'REMOTE_PORT not found';
return {
REQ_ID: req.id,
METHOD: method,
URL: url,
IP: ip,
HOSTNAME: hostname,
REMOTE_PORT: remotePort,
QUERY: query,
HEADERS: headers
};
};
const envToLogger = {
development: {
@@ -36,14 +54,21 @@ const envToLogger = {
},
level: FCC_API_LOG_LEVEL || 'info',
serializers: {
req: requestSerializer
req: (req: FastifyRequest) => {
return {
method: req.method,
url: req.url
};
}
}
// No need to redact in development
},
production: {
level: FCC_API_LOG_LEVEL || 'info',
serializers: {
req: requestSerializer
}
},
redact: ['req.HEADERS.cookie']
}
};