mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
Merge pull request #8738 from micalevisk/fix-issue-8733
fix(common,core): auto flush logs after `useLogger` call
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -142,6 +142,9 @@ export class NestFactoryStatic {
|
||||
const context = this.createNestInstance<NestApplicationContext>(
|
||||
new NestApplicationContext(container, [], root),
|
||||
);
|
||||
if (this.autoFlushLogs) {
|
||||
context.flushLogsOnOverride();
|
||||
}
|
||||
return context.init();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user