Skip to main content

Logger

@inversifyjs/logger is a versatile and extensible logging library designed for TypeScript applications. It provides a simple and consistent API for logging messages at various levels (e.g., info, warn, error) and supports multiple logging transports, including console, file, http and stream logging.

Using a logger

To use a logger in your application, just import the desired logger class and create an instance:

import { ConsoleLogger, Logger } from '@inversifyjs/logger';
import { inject, injectable } from 'inversify';

@injectable()
export class MyAwesomeService {
readonly #logger: Logger;
readonly #myAwesomeDependency: MyAwesomeDependency;

constructor(
@inject(MyAwesomeDependency)
myAwesomeDependency: MyAwesomeDependency,
) {
this.#logger = new ConsoleLogger('MyAwesomeService');
this.#myAwesomeDependency = myAwesomeDependency;
}

public doSomethingAwesome() {
this.#logger.info('Doing something awesome!');
this.#myAwesomeDependency.doSomething();
}
}
warning

You might be tempted to use DI to inject the logger, but this is not recommended. Reading context in the log traces such as the name of the module which sends the logs is useful, and having a shared instance of the logger would make this impossible.