mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-20 12:03:11 -04:00
fix(api): improve logging
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox';
|
||||
import { isEmpty } from 'lodash';
|
||||
|
||||
/**
|
||||
* Plugin for the health check endpoint.
|
||||
@@ -13,7 +14,26 @@ export const statusRoute: FastifyPluginCallbackTypebox = (
|
||||
_options,
|
||||
done
|
||||
) => {
|
||||
fastify.get('/status/ping', async (_req, _reply) => {
|
||||
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 params = isEmpty(req.params) ? 'PARAMS not found' : req.params;
|
||||
|
||||
fastify.log
|
||||
.child({
|
||||
URL: url,
|
||||
REQ_ID: reqId,
|
||||
HEADERS: headers,
|
||||
IP: ip,
|
||||
PARAMS: params
|
||||
})
|
||||
.debug('returning a ping');
|
||||
return { msg: 'pong' };
|
||||
});
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
// 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 { build } from './app';
|
||||
import {
|
||||
FREECODECAMP_NODE_ENV,
|
||||
@@ -11,25 +13,46 @@ 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 envToLogger = {
|
||||
development: {
|
||||
transport: {
|
||||
target: 'pino-pretty',
|
||||
options: {
|
||||
singleLine: true,
|
||||
translateTime: 'HH:MM:ss Z',
|
||||
ignore: 'pid,hostname'
|
||||
}
|
||||
},
|
||||
level: FCC_API_LOG_LEVEL || 'info'
|
||||
level: FCC_API_LOG_LEVEL || 'info',
|
||||
serializers: {
|
||||
req: requestSerializer
|
||||
}
|
||||
},
|
||||
production: {
|
||||
level: FCC_API_LOG_LEVEL || 'info'
|
||||
},
|
||||
test: undefined
|
||||
level: FCC_API_LOG_LEVEL || 'info',
|
||||
serializers: {
|
||||
req: requestSerializer
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const start = async () => {
|
||||
const fastify = await build({ logger: envToLogger[FREECODECAMP_NODE_ENV] });
|
||||
const fastify = await build({
|
||||
logger: envToLogger[FREECODECAMP_NODE_ENV] ?? true,
|
||||
genReqId: () => randomBytes(8).toString('hex'),
|
||||
disableRequestLogging: true
|
||||
});
|
||||
try {
|
||||
const port = Number(PORT);
|
||||
fastify.log.info(`Starting server on port ${port}`);
|
||||
|
||||
@@ -6,7 +6,7 @@ import { LogLevel } from 'fastify';
|
||||
const envPath = path.resolve(__dirname, '../../../.env');
|
||||
const { error } = config({ path: envPath });
|
||||
|
||||
if (error) {
|
||||
if (error && process.env.FREECODECAMP_NODE_ENV !== 'production') {
|
||||
console.warn(`
|
||||
----------------------------------------------------
|
||||
Warning: .env file not found.
|
||||
|
||||
Reference in New Issue
Block a user