Skip to content

Commit

Permalink
Merge pull request #8738 from micalevisk/fix-issue-8733
Browse files Browse the repository at this point in the history
fix(common,core): auto flush logs after `useLogger` call
  • Loading branch information
kamilmysliwiec committed Dec 16, 2021
2 parents bda3c64 + 0ae6f0e commit ffc0389
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions packages/core/nest-application-context.ts
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions packages/core/nest-factory.ts
Expand Up @@ -142,6 +142,9 @@ export class NestFactoryStatic {
const context = this.createNestInstance<NestApplicationContext>(
new NestApplicationContext(container, [], root),
);
if (this.autoFlushLogs) {
context.flushLogsOnOverride();
}
return context.init();
}

Expand Down

0 comments on commit ffc0389

Please sign in to comment.