From 201fbb220e403b919bb52bce0710c2a77335ed41 Mon Sep 17 00:00:00 2001 From: John Rassa Date: Wed, 11 Dec 2024 10:52:17 -0500 Subject: [PATCH] refactor: update fastify logging --- src/lib/fastify.ts | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/lib/fastify.ts b/src/lib/fastify.ts index 8ea926f..0d54b8c 100644 --- a/src/lib/fastify.ts +++ b/src/lib/fastify.ts @@ -15,10 +15,12 @@ import { fastify, FastifyInstance } from 'fastify'; import { glob } from 'glob'; import { Mongoose } from 'mongoose'; -import { logger } from './logger'; +import { logger as baseLogger } from './logger'; import pkg from '../../package.json'; import fastifyActuator from '../app/common/fastify/actuator'; +const logger = baseLogger.child({ component: 'fastify' }); + const baseApiPath = '/api'; export async function init(db: Mongoose) { @@ -99,15 +101,20 @@ async function initSession(app: FastifyInstance, db: Mongoose) { async function initModulesServerRoutes(app: FastifyInstance) { // Init the global route prefix - const routePaths = await glob(config.get('assets.controllers')); - const routes = await Promise.all( - routePaths.map((routePath: string) => import(path.posix.resolve(routePath))) + const controllerPaths = await glob( + config.get('assets.controllers') + ); + logger.info(`Registering ${controllerPaths.length} controllers`); + + await Promise.all( + controllerPaths.map(async (controllerPath: string) => { + const controller = await import(path.posix.resolve(controllerPath)); + if (controller.default) { + logger.debug(`Registering controller: ${controllerPath}`); + app.register(controller.default, { prefix: baseApiPath }); + } + }) ); - routes - .filter((route) => route.default) - .forEach((route) => { - app.register(route.default, { prefix: baseApiPath }); - }); } function initSwaggerAPI(app: FastifyInstance) { @@ -116,7 +123,7 @@ function initSwaggerAPI(app: FastifyInstance) { return; } - app.log.info('Configuring api docs'); + logger.info('Configuring api docs'); app.register(fastifySwagger, { openapi: { @@ -143,7 +150,7 @@ function initActuator(app: FastifyInstance) { return; } - app.log.info('Configuring actuator endpoints'); + logger.info('Configuring actuator endpoints'); const basePath = config.get('actuator.options.basePath'); app.register(fastifyActuator, {