Merge pull request #8738 from micalevisk/fix-issue-8733

fix(common,core): auto flush logs after `useLogger` call
This commit is contained in:
Kamil Mysliwiec
2021-12-16 14:11:21 +01:00
committed by GitHub
3 changed files with 17 additions and 1 deletions

View File

@@ -50,7 +50,8 @@ export interface INestApplicationContext {
close(): Promise<void>;
/**
* Sets custom logger service
* Sets custom logger service.
* Flushes buffered logs if auto flush is on.
* @returns {void}
*/
useLogger(logger: LoggerService | LogLevel[] | false): void;

View File

@@ -35,6 +35,7 @@ export class NestApplicationContext implements INestApplicationContext {
protected isInitialized = false;
protected readonly injector = new Injector();
private shouldFlushLogsOnOverride = false;
private readonly activeShutdownSignals = new Array<string>();
private readonly moduleCompiler = new ModuleCompiler();
private shutdownCleanupRef?: (...args: unknown[]) => unknown;
@@ -131,12 +132,23 @@ export class NestApplicationContext implements INestApplicationContext {
public useLogger(logger: LoggerService | LogLevel[] | false) {
Logger.overrideLogger(logger);
if (this.shouldFlushLogsOnOverride) {
this.flushLogs();
}
}
public flushLogs() {
Logger.flush();
}
/**
* Define that it must flush logs right after defining a custom logger.
*/
public flushLogsOnOverride() {
this.shouldFlushLogsOnOverride = true;
}
/**
* Enables the usage of shutdown hooks. Will call the
* `onApplicationShutdown` function of a provider if the

View File

@@ -142,6 +142,9 @@ export class NestFactoryStatic {
const context = this.createNestInstance<NestApplicationContext>(
new NestApplicationContext(container, [], root),
);
if (this.autoFlushLogs) {
context.flushLogsOnOverride();
}
return context.init();
}