Skip to content

Commit

Permalink
feat(di): add attach logger function util to create custom DI container
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed May 21, 2023
1 parent 09c51a5 commit 9e0d8ae
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/di/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<CalendarCtrl>();

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();
}
Expand Down
1 change: 1 addition & 0 deletions packages/di/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
14 changes: 14 additions & 0 deletions packages/di/src/utils/attachLogger.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
8 changes: 8 additions & 0 deletions packages/di/src/utils/attachLogger.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 9e0d8ae

Please sign in to comment.