fix(backend): immediately use our custom logger

While the DI and database initialization is running, NestJSs default logger is normally used. Our custom logger was only being initialized after DI setup is complete.
Errors encountered during DI setup were buffered and only printed after DI init was complete, or the app exited on error.
This led to the app not printing anything for a minute in certain cases.

This commit replaces the initial logger with our ConsoleLoggerService that logs everything.
After DI init is complete, that logger is replaced with a normal instance of ConsoleLoggerService that uses the real config from DI.

Fixes https://github.com/hedgedoc/hedgedoc/issues/4306

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2023-06-25 21:29:50 +02:00
parent 7f5bc46846
commit 4d50f2ec33

View file

@ -3,7 +3,6 @@
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { LogLevel } from '@nestjs/common';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core'; import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express'; import { NestExpressApplication } from '@nestjs/platform-express';
@ -12,14 +11,16 @@ import { setupApp } from './app-init';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
import { AppConfig } from './config/app.config'; import { AppConfig } from './config/app.config';
import { AuthConfig } from './config/auth.config'; import { AuthConfig } from './config/auth.config';
import { Loglevel } from './config/loglevel.enum';
import { MediaConfig } from './config/media.config'; import { MediaConfig } from './config/media.config';
import { ConsoleLoggerService } from './logger/console-logger.service'; import { ConsoleLoggerService } from './logger/console-logger.service';
async function bootstrap(): Promise<void> { async function bootstrap(): Promise<void> {
// Initialize AppModule // Initialize AppModule
const app = await NestFactory.create<NestExpressApplication>(AppModule, { const app = await NestFactory.create<NestExpressApplication>(AppModule, {
logger: ['error', 'warn', 'log'] as LogLevel[], // ConsoleLoggerService only uses the loglevel, so we can give it an incomplete AppConfig to log everything
bufferLogs: true, // This Logger instance will be replaced by a proper one with config from DI below
logger: new ConsoleLoggerService({ loglevel: Loglevel.TRACE } as AppConfig),
}); });
// Set up our custom logger // Set up our custom logger