Switch to new buffered logger

NestJS 8 allows the logs to buffer on startup, so that all logs run
through our custom logger.

See also https://docs.nestjs.com/techniques/logger#dependency-injection

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-07-15 20:21:42 +02:00
parent 2a382d0fe6
commit ab231e0f6e
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 9 additions and 2 deletions

View file

@ -4,7 +4,13 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { Inject, Injectable, Optional, Scope } from '@nestjs/common'; import {
Inject,
Injectable,
LoggerService,
Optional,
Scope,
} from '@nestjs/common';
import { isObject } from '@nestjs/common/utils/shared.utils'; import { isObject } from '@nestjs/common/utils/shared.utils';
import clc = require('cli-color'); import clc = require('cli-color');
import DateTimeFormatOptions = Intl.DateTimeFormatOptions; import DateTimeFormatOptions = Intl.DateTimeFormatOptions;
@ -13,7 +19,7 @@ import { Loglevel } from '../config/loglevel.enum';
import { needToLog } from '../config/utils'; import { needToLog } from '../config/utils';
@Injectable({ scope: Scope.TRANSIENT }) @Injectable({ scope: Scope.TRANSIENT })
export class ConsoleLoggerService { export class ConsoleLoggerService implements LoggerService {
private classContext: string | undefined; private classContext: string | undefined;
private lastTimestamp: number; private lastTimestamp: number;

View file

@ -18,6 +18,7 @@ import { ConsoleLoggerService } from './logger/console-logger.service';
async function bootstrap(): Promise<void> { async function bootstrap(): Promise<void> {
const app = await NestFactory.create<NestExpressApplication>(AppModule, { const app = await NestFactory.create<NestExpressApplication>(AppModule, {
logger: ['error', 'warn', 'log'] as LogLevel[], logger: ['error', 'warn', 'log'] as LogLevel[],
bufferLogs: true,
}); });
const logger = await app.resolve(ConsoleLoggerService); const logger = await app.resolve(ConsoleLoggerService);
logger.log('Switching logger', 'AppBootstrap'); logger.log('Switching logger', 'AppBootstrap');