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

fix(common,core): auto flush logs after useLogger call #8738

Merged
merged 3 commits into from Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
13 changes: 13 additions & 0 deletions packages/core/nest-application-context.ts
Expand Up @@ -35,6 +35,8 @@ export class NestApplicationContext implements INestApplicationContext {
protected isInitialized = false;
protected readonly injector = new Injector();

private shouldFlushLogsOnOverride = false;
kamilmysliwiec marked this conversation as resolved.
Show resolved Hide resolved

micalevisk marked this conversation as resolved.
Show resolved Hide resolved
kamilmysliwiec marked this conversation as resolved.
Show resolved Hide resolved
private readonly activeShutdownSignals = new Array<string>();
private readonly moduleCompiler = new ModuleCompiler();
private shutdownCleanupRef?: (...args: unknown[]) => unknown;
Expand Down Expand Up @@ -131,12 +133,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