Logger User
As a developer, you can use the service offered by ZanobiJS to print information to the console.
There are two types of console prints:
- Framework debug prints.
- User-decided prints.
To activate these, you need to include the factory options:
const factory = new Factory(AppModule, { activeLoggerSystem: true, activeLoggerUser: true,});
When we enable activeLoggerSystem, it allows the printing of the debug logs that the framework makes internally.
Meanwhile, activeLoggerUser allows enabling the debugging that the user decides to display in the console.
import { Injectable } from "@zanobijs/common";import { LoggerUser } from "@zanobijs/common/utils";const logUSer = LoggerUser();
@Injectable()export class ServiceExample { constructor() {} getHello(): void { logUSer.success("Lorem ipsum success"); logUSer.info("Lorem ipsum info"); logUSer.warn("Lorem ipsum warn"); logUSer.error("Lorem ipsum error"); logUSer.debug("Lorem ipsum debug"); logUSer.important("Lorem important"); } // ***** PRINT ***** // [SUCCESS]: Lorem ipsum success <prints on console with green color> // [INFO]: Lorem ipsum info <prints on console with blue color> // [WARN]: Lorem ipsum warn <prints on console with yellow color> // [ERROR]: Lorem ipsum error <prints on console with red color> // [DEBUG]: Lorem ipsum debug <prints on console with white color> // [IMPORTANT]: Lorem important <prints on console with red background>}
Printing with or without color
const logUser = LoggerUser({ withColor: false });
Injecting the logger
You can also inject the logger to be used in controllers or services within the application.
import { Module } from "@zanobijs/common";import { ServiceA } from "./A.service";
@Module({ imports: [], controllers: [], services: [ ServiceA, { provider: "LOG", useValue: LoggerUser( { withColor: false })}, // Remember, if you want the print to be in color, use LoggerUser() // Without options or LoggerUser({ withColor: true }), as you prefer ], exports: [],})export class AppModule {}
import { Injectable, ILoggerService } from "@zanobijs/common";
@Injectable()export class ServiceA { constructor(@Inject("LOG") private log: ILoggerService) {}
getHello() { this.log.success("print me log", { test: "some" }); // Remember that the console print will be without colors { withColor: false } return "Hello ServiceExample"; }}
Support us
ZanobiJS is an open source project licensed by MIT. It can grow thanks to the support of amazing people like you. If you want to join, read more here.