diff --git a/packages/di/readme.md b/packages/di/readme.md index 092adbf3177..777129f4d14 100644 --- a/packages/di/readme.md +++ b/packages/di/readme.md @@ -118,19 +118,25 @@ export class CalendarCtrl { Finally, we can load the injector and use: ```typescript -import {InjectorService} from "@tsed/di"; +import {InjectorService, attachLogger} from "@tsed/di"; +import {$log} from "@tsed/logger"; import {CalendarCtrl} from "./CalendarCtrl"; async function bootstrap() { const injector = new InjectorService(); + // configure the default logger + attachLogger(injector, $log); + // Load all providers registered via @Injectable decorator await injector.load(); const calendarController = injector.get(); - await calendarController.create(new Calendar()); + // emit event to trigger actions for third parties modules + await injector.emit("$onReady"); + // And finally destroy injector and his instances (see injector hooks) await injector.destroy(); } diff --git a/packages/di/src/index.ts b/packages/di/src/index.ts index 5f3078d151e..df482d2ee68 100644 --- a/packages/di/src/index.ts +++ b/packages/di/src/index.ts @@ -53,6 +53,7 @@ export * from "./services/DILogger"; export * from "./services/DITest"; export * from "./services/InjectorService"; export * from "./utils/asyncHookContext"; +export * from "./utils/attachLogger"; export * from "./utils/colors"; export * from "./utils/createContainer"; export * from "./utils/getConfiguration"; diff --git a/packages/di/src/utils/attachLogger.spec.ts b/packages/di/src/utils/attachLogger.spec.ts new file mode 100644 index 00000000000..bb389c1daf1 --- /dev/null +++ b/packages/di/src/utils/attachLogger.spec.ts @@ -0,0 +1,14 @@ +import {Logger} from "@tsed/logger"; +import {InjectorService} from "../services/InjectorService"; +import {attachLogger} from "./attachLogger"; + +describe("attachLogger", () => { + it("should attach logger", () => { + const injector = new InjectorService(); + const $log = new Logger("test"); + + attachLogger(injector, $log); + + expect(injector.logger).toEqual($log); + }); +}); diff --git a/packages/di/src/utils/attachLogger.ts b/packages/di/src/utils/attachLogger.ts new file mode 100644 index 00000000000..e8da87551b9 --- /dev/null +++ b/packages/di/src/utils/attachLogger.ts @@ -0,0 +1,8 @@ +import type {Logger} from "@tsed/logger"; +import {InjectorService} from "../services/InjectorService"; +import {setLoggerConfiguration} from "./setLoggerConfiguration"; + +export function attachLogger(injector: InjectorService, $log: Logger) { + injector.logger = $log; + setLoggerConfiguration(injector); +}