Skip to main content

Logging

Logger is a class that provides a set of methods to log messages using native node modules. It is a simple way to log messages in your application and it is already included in the ExpressoTS framework.

Logger take two parameters:

  • message: The message to be logged.
  • module: The service name associated with the log.

Logger methods

There are 4 methods available:

Logger usage
log.<method>(message: string, module?: string);
  • info: Logs an informational message.
  • warn: Logs a warning message.
  • error: Logs an error message.
  • msg: Logs a message.

Logger format

Using info() method as an example, the logger format is:

[ExpressoTS] 00/00/0000 00:00:00 PM [PID:30319] INFO [app-controller] Hello from Expressots!

Register logger in the container

The logger is already included in the ExpressoTS framework but first you need to register it in the container. You can do this in the configureServices method of your application.

export class App extends AppExpress {
private middleware: IMiddleware;
private provider: ProviderManager;

constructor() {
super();
this.middleware = container.get<IMiddleware>(Middleware);
this.provider = container.get(ProviderManager);
}

protected configureServices(): void | Promise<void> {
this.provider.register(Logger);
}
}

Usage example

Once the logger is registered in the container, you can inject it into your services, controllers, use cases, etc.

export class YourUseUseCase {
constructor(private log: Logger) {}

execute() {
this.log.info("Testing log message", "your-use-case");
return "Hello ExpressoTS!";
}
}

Support us ❤️

ExpressoTS is an MIT-licensed open source project. It's an independent project with ongoing development made possible thanks to your support. If you'd like to help, please read our support guide.