diff --git a/apps/server/src/apps/admin-api-server.app.ts b/apps/server/src/apps/admin-api-server.app.ts index bb0f72f067..9a9b9b2306 100644 --- a/apps/server/src/apps/admin-api-server.app.ts +++ b/apps/server/src/apps/admin-api-server.app.ts @@ -6,7 +6,7 @@ import { LegacyLogger, Logger } from '@src/core/logger'; import { AdminApiServerModule } from '@modules/server/admin-api.server.module'; import express from 'express'; import { install as sourceMapInstall } from 'source-map-support'; -import { createAppLoggerMiddleware } from '@src/apps/helpers/app-logger-middleware'; +import { createRequestLoggerMiddleware } from './helpers/request-logger-middleware'; import { AppStartLoggable, enableOpenApiDocs, @@ -24,7 +24,7 @@ async function bootstrap() { const nestAdminServerApp = await NestFactory.create(AdminApiServerModule, nestAdminServerExpressAdapter); const logger = await nestAdminServerApp.resolve(Logger); const legacyLogger = await nestAdminServerApp.resolve(LegacyLogger); - nestAdminServerApp.use(createAppLoggerMiddleware(await nestAdminServerApp.resolve(Logger))); + nestAdminServerApp.use(createRequestLoggerMiddleware(await nestAdminServerApp.resolve(Logger))); nestAdminServerApp.useLogger(legacyLogger); nestAdminServerApp.enableCors(); diff --git a/apps/server/src/apps/board-collaboration.app.ts b/apps/server/src/apps/board-collaboration.app.ts index 3cea7f4921..b84ae6b24c 100644 --- a/apps/server/src/apps/board-collaboration.app.ts +++ b/apps/server/src/apps/board-collaboration.app.ts @@ -12,7 +12,7 @@ import { RedisIoAdapter } from '@infra/socketio'; import { BoardCollaborationModule } from '@modules/board/board-collaboration.app.module'; import express from 'express'; import { ExpressAdapter } from '@nestjs/platform-express'; -import { createAppLoggerMiddleware } from '@src/apps/helpers/app-logger-middleware'; +import { createRequestLoggerMiddleware } from './helpers/request-logger-middleware'; import { enableOpenApiDocs, addPrometheusMetricsMiddlewaresIfEnabled, @@ -38,7 +38,7 @@ async function bootstrap() { }; enableOpenApiDocs(nestApp, 'docs', options); const logger = await nestApp.resolve(Logger); - nestApp.use(createAppLoggerMiddleware(await nestApp.resolve(Logger))); + nestApp.use(createRequestLoggerMiddleware(await nestApp.resolve(Logger))); await nestApp.init(); diff --git a/apps/server/src/apps/common-cartridge.app.ts b/apps/server/src/apps/common-cartridge.app.ts index 5ac0a20706..84444508ed 100644 --- a/apps/server/src/apps/common-cartridge.app.ts +++ b/apps/server/src/apps/common-cartridge.app.ts @@ -6,7 +6,7 @@ import { LegacyLogger, Logger } from '@src/core/logger'; import { CommonCartridgeApiModule } from '@modules/common-cartridge/common-cartridge-api.module'; import express from 'express'; import { install as sourceMapInstall } from 'source-map-support'; -import { createAppLoggerMiddleware } from '@src/apps/helpers/app-logger-middleware'; +import { createRequestLoggerMiddleware } from './helpers/request-logger-middleware'; import { AppStartLoggable, enableOpenApiDocs, @@ -28,7 +28,7 @@ async function bootstrap() { const rootExpress = express(); const logger = await nestApp.resolve(Logger); - nestApp.use(createAppLoggerMiddleware(await nestApp.resolve(Logger))); + nestApp.use(createRequestLoggerMiddleware(await nestApp.resolve(Logger))); addPrometheusMetricsMiddlewaresIfEnabled(logger, rootExpress); diff --git a/apps/server/src/apps/files-storage.app.ts b/apps/server/src/apps/files-storage.app.ts index 683a8c6c37..2244713094 100644 --- a/apps/server/src/apps/files-storage.app.ts +++ b/apps/server/src/apps/files-storage.app.ts @@ -12,7 +12,7 @@ import { FilesStorageApiModule } from '@modules/files-storage/files-storage-api. import { API_VERSION_PATH } from '@modules/files-storage/files-storage.const'; import { SwaggerDocumentOptions } from '@nestjs/swagger'; import { LegacyLogger, Logger } from '@src/core/logger'; -import { createAppLoggerMiddleware } from '@src/apps/helpers/app-logger-middleware'; +import { createRequestLoggerMiddleware } from './helpers/request-logger-middleware'; import { enableOpenApiDocs } from './helpers'; async function bootstrap() { @@ -29,7 +29,7 @@ async function bootstrap() { // customize nest app settings nestApp.enableCors({ exposedHeaders: ['Content-Disposition'] }); - nestApp.use(createAppLoggerMiddleware(await nestApp.resolve(Logger))); + nestApp.use(createRequestLoggerMiddleware(await nestApp.resolve(Logger))); const options: SwaggerDocumentOptions = { operationIdFactory: (_controllerKey: string, methodKey: string) => methodKey, diff --git a/apps/server/src/apps/fwu-learning-contents.app.ts b/apps/server/src/apps/fwu-learning-contents.app.ts index 08ed85a038..8f71e94255 100644 --- a/apps/server/src/apps/fwu-learning-contents.app.ts +++ b/apps/server/src/apps/fwu-learning-contents.app.ts @@ -10,7 +10,7 @@ import { install as sourceMapInstall } from 'source-map-support'; // application imports import { LegacyLogger, Logger } from '@src/core/logger'; import { FwuLearningContentsModule } from '@modules/fwu-learning-contents'; -import { createAppLoggerMiddleware } from '@src/apps/helpers/app-logger-middleware'; +import { createRequestLoggerMiddleware } from './helpers/request-logger-middleware'; import { enableOpenApiDocs } from './helpers'; async function bootstrap() { @@ -27,7 +27,7 @@ async function bootstrap() { // customize nest app settings nestApp.enableCors({ exposedHeaders: ['Content-Disposition'] }); enableOpenApiDocs(nestApp, 'docs'); - nestApp.use(createAppLoggerMiddleware(await nestApp.resolve(Logger))); + nestApp.use(createRequestLoggerMiddleware(await nestApp.resolve(Logger))); await nestApp.init(); diff --git a/apps/server/src/apps/h5p-editor.app.ts b/apps/server/src/apps/h5p-editor.app.ts index d113dd8416..32682c94bc 100644 --- a/apps/server/src/apps/h5p-editor.app.ts +++ b/apps/server/src/apps/h5p-editor.app.ts @@ -10,7 +10,7 @@ import { install as sourceMapInstall } from 'source-map-support'; // application imports import { LegacyLogger, Logger } from '@src/core/logger'; import { H5PEditorModule } from '@modules/h5p-editor'; -import { createAppLoggerMiddleware } from '@src/apps/helpers/app-logger-middleware'; +import { createRequestLoggerMiddleware } from './helpers/request-logger-middleware'; import { enableOpenApiDocs } from './helpers'; async function bootstrap() { @@ -25,7 +25,7 @@ async function bootstrap() { // WinstonLogger nestApp.useLogger(await nestApp.resolve(LegacyLogger)); - nestApp.use(createAppLoggerMiddleware(await nestApp.resolve(Logger))); + nestApp.use(createRequestLoggerMiddleware(await nestApp.resolve(Logger))); // customize nest app settings nestApp.enableCors({ exposedHeaders: ['Content-Disposition'] }); diff --git a/apps/server/src/apps/helpers/logger-middleware.ts b/apps/server/src/apps/helpers/logger-middleware.ts deleted file mode 100644 index def3a0714e..0000000000 --- a/apps/server/src/apps/helpers/logger-middleware.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Request, Response, NextFunction } from 'express'; -import { Injectable, NestMiddleware, Logger } from '@nestjs/common'; - -@Injectable() -export class LoggerMiddleware implements NestMiddleware { - private logger = new Logger('HTTP'); - - public use(request: Request, response: Response, next: NextFunction): void { - const startAt = process.hrtime(); - const { method, originalUrl } = request; - - response.on('finish', () => { - const { statusCode } = response; - const contentLength = response.get('content-length') || 'unknown'; - const diff = process.hrtime(startAt); - const responseTime = diff[0] * 1e3 + diff[1] * 1e-6; - this.logger.log(`${method} ${originalUrl} ${statusCode} ${responseTime}ms ${contentLength}`); - }); - - next(); - } -} \ No newline at end of file diff --git a/apps/server/src/apps/helpers/app-logger-middleware.ts b/apps/server/src/apps/helpers/request-logger-middleware.ts similarity index 95% rename from apps/server/src/apps/helpers/app-logger-middleware.ts rename to apps/server/src/apps/helpers/request-logger-middleware.ts index a2fdbf08f7..488c05fb74 100644 --- a/apps/server/src/apps/helpers/app-logger-middleware.ts +++ b/apps/server/src/apps/helpers/request-logger-middleware.ts @@ -3,7 +3,7 @@ import { Logger } from '@src/core/logger'; import { RequestLoggable } from '@src/apps/helpers/request-loggable'; import { Configuration } from '@hpi-schul-cloud/commons/lib'; -export const createAppLoggerMiddleware = ( +export const createRequestLoggerMiddleware = ( logger: Logger ): ((request: Request, response: Response, next: NextFunction) => void) => { logger.setContext('AppLoggerMiddleware'); diff --git a/apps/server/src/apps/management.app.ts b/apps/server/src/apps/management.app.ts index 9f78e55a93..593fa41101 100644 --- a/apps/server/src/apps/management.app.ts +++ b/apps/server/src/apps/management.app.ts @@ -10,7 +10,7 @@ import { install as sourceMapInstall } from 'source-map-support'; // application imports import { LegacyLogger, Logger } from '@src/core/logger'; import { ManagementServerModule } from '@modules/management'; -import { createAppLoggerMiddleware } from '@src/apps/helpers/app-logger-middleware'; +import { createRequestLoggerMiddleware } from './helpers/request-logger-middleware'; import { enableOpenApiDocs } from './helpers'; async function bootstrap() { @@ -22,7 +22,7 @@ async function bootstrap() { const nestExpressAdapter = new ExpressAdapter(nestExpress); const nestApp = await NestFactory.create(ManagementServerModule, nestExpressAdapter); - nestApp.use(createAppLoggerMiddleware(await nestApp.resolve(Logger))); + nestApp.use(createRequestLoggerMiddleware(await nestApp.resolve(Logger))); // WinstonLogger nestApp.useLogger(await nestApp.resolve(LegacyLogger)); diff --git a/apps/server/src/apps/server.app.ts b/apps/server/src/apps/server.app.ts index 4fdac329b9..f2e5de6a21 100644 --- a/apps/server/src/apps/server.app.ts +++ b/apps/server/src/apps/server.app.ts @@ -23,7 +23,7 @@ import { join } from 'path'; // register source-map-support for debugging import { install as sourceMapInstall } from 'source-map-support'; -import { createAppLoggerMiddleware } from '@src/apps/helpers/app-logger-middleware'; +import { createRequestLoggerMiddleware } from './helpers/request-logger-middleware'; import { AppStartLoggable, enableOpenApiDocs, @@ -46,7 +46,7 @@ async function bootstrap() { nestApp.useLogger(legacyLogger); const logger = await nestApp.resolve(Logger); - nestApp.use(createAppLoggerMiddleware(await nestApp.resolve(Logger))); + nestApp.use(createRequestLoggerMiddleware(await nestApp.resolve(Logger))); // load the legacy feathers/express server // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment