Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC-8556 implement request logging in nestjs #5401

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions apps/server/src/apps/admin-api-server.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { AdminApiServerModule } from '@modules/server/admin-api.server.module';
import express from 'express';
import { install as sourceMapInstall } from 'source-map-support';
import { createRequestLoggerMiddleware } from './helpers/request-logger-middleware';
import {
AppStartLoggable,
enableOpenApiDocs,
Expand All @@ -13,7 +14,7 @@
createAndStartPrometheusMetricsAppIfEnabled,
} from './helpers';

async function bootstrap() {

Check warning on line 17 in apps/server/src/apps/admin-api-server.app.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Missing return type on function
sourceMapInstall();

const nestAdminServerExpress = express();
Expand All @@ -23,6 +24,8 @@
const nestAdminServerApp = await NestFactory.create(AdminApiServerModule, nestAdminServerExpressAdapter);
const logger = await nestAdminServerApp.resolve(Logger);
const legacyLogger = await nestAdminServerApp.resolve(LegacyLogger);
nestAdminServerApp.use(createRequestLoggerMiddleware());

nestAdminServerApp.useLogger(legacyLogger);
nestAdminServerApp.enableCors();

Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/apps/board-collaboration.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
// application imports
import { SwaggerDocumentOptions } from '@nestjs/swagger';
import { LegacyLogger, Logger } from '@src/core/logger';
import { RedisIoAdapter } from '@infra/socketio';

Check warning on line 11 in apps/server/src/apps/board-collaboration.app.ts

View workflow job for this annotation

GitHub Actions / nest_lint

'@infra/socketio' import is restricted from being used by a pattern. apps-modules may NOT import from @apps, @infra or @shared
import { BoardCollaborationModule } from '@modules/board/board-collaboration.app.module';
import express from 'express';
import { ExpressAdapter } from '@nestjs/platform-express';
import { createRequestLoggerMiddleware } from './helpers/request-logger-middleware';
import {
enableOpenApiDocs,
addPrometheusMetricsMiddlewaresIfEnabled,
createAndStartPrometheusMetricsAppIfEnabled,
} from './helpers';

async function bootstrap() {

Check warning on line 22 in apps/server/src/apps/board-collaboration.app.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Missing return type on function
sourceMapInstall();

const nestExpress = express();
Expand All @@ -37,6 +38,7 @@
};
enableOpenApiDocs(nestApp, 'docs', options);
const logger = await nestApp.resolve(Logger);
nestApp.use(createRequestLoggerMiddleware());

await nestApp.init();

Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/apps/common-cartridge.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { CommonCartridgeApiModule } from '@modules/common-cartridge/common-cartridge-api.module';
import express from 'express';
import { install as sourceMapInstall } from 'source-map-support';
import { createRequestLoggerMiddleware } from './helpers/request-logger-middleware';
import {
AppStartLoggable,
enableOpenApiDocs,
Expand All @@ -13,7 +14,7 @@
createAndStartPrometheusMetricsAppIfEnabled,
} from './helpers';

async function bootstrap() {

Check warning on line 17 in apps/server/src/apps/common-cartridge.app.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Missing return type on function
sourceMapInstall();

const nestExpress = express();
Expand All @@ -27,6 +28,7 @@

const rootExpress = express();
const logger = await nestApp.resolve(Logger);
nestApp.use(createRequestLoggerMiddleware());

addPrometheusMetricsMiddlewaresIfEnabled(logger, rootExpress);

Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/apps/files-storage.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
import { API_VERSION_PATH } from '@modules/files-storage/files-storage.const';
import { SwaggerDocumentOptions } from '@nestjs/swagger';
import { LegacyLogger } from '@src/core/logger';
import { createRequestLoggerMiddleware } from './helpers/request-logger-middleware';
import { enableOpenApiDocs } from './helpers';

async function bootstrap() {

Check warning on line 18 in apps/server/src/apps/files-storage.app.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Missing return type on function
sourceMapInstall();

// create the NestJS application on a seperate express instance
Expand All @@ -28,6 +29,7 @@

// customize nest app settings
nestApp.enableCors({ exposedHeaders: ['Content-Disposition'] });
nestApp.use(createRequestLoggerMiddleware());

const options: SwaggerDocumentOptions = {
operationIdFactory: (_controllerKey: string, methodKey: string) => methodKey,
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/apps/fwu-learning-contents.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
// application imports
import { LegacyLogger } from '@src/core/logger';
import { FwuLearningContentsModule } from '@modules/fwu-learning-contents';
import { createRequestLoggerMiddleware } from './helpers/request-logger-middleware';
import { enableOpenApiDocs } from './helpers';

async function bootstrap() {

Check warning on line 16 in apps/server/src/apps/fwu-learning-contents.app.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Missing return type on function
sourceMapInstall();

// create the NestJS application on a seperate express instance
Expand All @@ -26,6 +27,7 @@
// customize nest app settings
nestApp.enableCors({ exposedHeaders: ['Content-Disposition'] });
enableOpenApiDocs(nestApp, 'docs');
nestApp.use(createRequestLoggerMiddleware());

await nestApp.init();

Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/apps/h5p-editor.app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* istanbul ignore file */
/* eslint-disable no-console */
import { NestFactory } from '@nestjs/core';

Check warning on line 3 in apps/server/src/apps/h5p-editor.app.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Filename 'h5p-editor.app.ts' does not match kebabcase
import { ExpressAdapter } from '@nestjs/platform-express';
import express from 'express';

Expand All @@ -10,9 +10,10 @@
// application imports
import { LegacyLogger } from '@src/core/logger';
import { H5PEditorModule } from '@modules/h5p-editor';
import { createRequestLoggerMiddleware } from './helpers/request-logger-middleware';
import { enableOpenApiDocs } from './helpers';

async function bootstrap() {

Check warning on line 16 in apps/server/src/apps/h5p-editor.app.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Missing return type on function
sourceMapInstall();

// create the NestJS application on a seperate express instance
Expand All @@ -24,6 +25,8 @@
// WinstonLogger
nestApp.useLogger(await nestApp.resolve(LegacyLogger));

nestApp.use(createRequestLoggerMiddleware());

// customize nest app settings
nestApp.enableCors({ exposedHeaders: ['Content-Disposition'] });
enableOpenApiDocs(nestApp, 'docs');
Expand Down
33 changes: 33 additions & 0 deletions apps/server/src/apps/helpers/request-logger-middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Request, Response, NextFunction } from 'express';
import { Configuration } from '@hpi-schul-cloud/commons/lib';
import { Logger } from '@nestjs/common';

export const createRequestLoggerMiddleware = (): ((
request: Request,
response: Response,
next: NextFunction
) => void) => {
const enabled = Configuration.get('REQUEST_LOGGING_ENABLED') as boolean;
const logger = new Logger('REQUEST_LOG');

return (request: Request, response: Response, next: NextFunction): void => {
Loki-Afro marked this conversation as resolved.
Show resolved Hide resolved
if (enabled) {
const startAt = process.hrtime();
const { method, originalUrl } = request;

response.on('finish', () => {
try {
const { statusCode } = response;
const contentLength = response.get('content-length') || 'unknown';
const diff = process.hrtime(startAt);
const responseTime = diff[0] * 1e3 + diff[1] * 1e-6;
logger.log(`${method} ${originalUrl} ${statusCode} ${responseTime}ms ${contentLength}`);
} catch (error) {
logger.error('unable to write accesslog', error);
}
});
}

next();
};
};
5 changes: 4 additions & 1 deletion apps/server/src/apps/management.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ import { install as sourceMapInstall } from 'source-map-support';
// application imports
import { LegacyLogger } from '@src/core/logger';
import { ManagementServerModule } from '@modules/management';
import { createRequestLoggerMiddleware } from './helpers/request-logger-middleware';
import { enableOpenApiDocs } from './helpers';

async function bootstrap() {
sourceMapInstall();

// create the NestJS application on a seperate express instance
// create the NestJS application on a separate express instance
const nestExpress = express();

const nestExpressAdapter = new ExpressAdapter(nestExpress);
const nestApp = await NestFactory.create(ManagementServerModule, nestExpressAdapter);

nestApp.use(createRequestLoggerMiddleware());

// WinstonLogger
nestApp.useLogger(await nestApp.resolve(LegacyLogger));

Expand Down
4 changes: 3 additions & 1 deletion apps/server/src/apps/server.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { join } from 'path';

// register source-map-support for debugging
import { install as sourceMapInstall } from 'source-map-support';
import { createRequestLoggerMiddleware } from './helpers/request-logger-middleware';
import {
AppStartLoggable,
enableOpenApiDocs,
Expand All @@ -34,7 +35,7 @@ import legacyAppPromise = require('../../../../src/app');
async function bootstrap() {
sourceMapInstall();

// create the NestJS application on a seperate express instance
// create the NestJS application on a separate express instance
const nestExpress = express();
const nestExpressAdapter = new ExpressAdapter(nestExpress);
const nestApp = await NestFactory.create(ServerModule, nestExpressAdapter);
Expand All @@ -45,6 +46,7 @@ async function bootstrap() {
nestApp.useLogger(legacyLogger);

const logger = await nestApp.resolve(Logger);
nestApp.use(createRequestLoggerMiddleware());

// load the legacy feathers/express server
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
Expand Down
Loading