Basic of NestLogged is to add a log at the call and return time of the method.
You can use LoggedRoute decorator for methods in Controller, and LoggedFunction for methods in other classes like service to simply add call and return logs and print out the logs.
// Controller
import { LoggedRoute } from 'nestlogged';
import { Controller, Get } from '@nestjs/common';
@Controller('cats')
export class CatsController {
@LoggedRoute()
@Get()
public async findAllCats() {
// ...
}
}
/*
LoggedRoute Log Message:
[Nest] 4208 - 01/12/2024, 5:16:02 PM LOG [CatsController] HIT HTTP CatsController::/[GET] (findAllCats)
...
[Nest] 4208 - 01/12/2024, 5:16:02 PM LOG [CatsController] RETURNED HTTP CatsController::/[GET] (findAllCats)
*/
// Service
import { LoggedFunction } from 'nestlogged';
import { Injectable } from '@nestjs/common';
@Injectable()
export class CatsService {
@LoggedFunction()
public async findAllCats() {
// ...
}
}
/*
LoggedFunction Log Message:
[Nest] 23564 - 01/12/2024, 5:17:40 PM LOG [CatsService] CALL findAllCats
...
[Nest] 23564 - 01/12/2024, 5:17:40 PM LOG [CatsService] RETURNED findAllCats
*/
Decorator Options
You can put option object to set decorator's settings.
Both decorator takes same type of option. You can see the definition of option and default values in below.
For LoggedFunction and LoggedRoute decorator, it initializes Logger in the class (use logger property if exists) at decorator call, and create new ScopedLogger to use it for logging.
ScopedLogger is a new logger that we implemented for scoped logging. but, it just adds a scope in the format of the log, not using a whole new system for logging. Internally, it still uses Logger class exported in the @nestjs/common package.
LoggedRoute includes route parameter in the log, or use route parameter from nestjs decorators like @Get or @Post (default /).
For instance, in the example below, the route parameter in the LoggedRoute decorator is not filled and also in the Get decorator, so the route in the log will be /.
@Controller('cats')
export class CatsController {
@LoggedRoute()
@Get()
public async findAllCats() {
// ...
}
}
/*
LoggedRoute Log Message:
route
v
... HIT HTTP CatsController::/[GET] (findAllCats)
...
route
v
... RETURNED HTTP CatsController::/[GET] (findAllCats)
*/
Providing '/findall' value in route parameter of the Get decorator will make the route in the log /findall.
@Controller('cats')
export class CatsController {
@LoggedRoute()
@Get('/findall') // 'findall' route
public async findAllCats() {
// ...
}
}
/*
LoggedRoute Log Message:
route
vvvvvvvv
... HIT HTTP CatsController::/findall[GET] (findAllCats)
...
route
vvvvvvvv
... RETURNED HTTP CatsController::/findall[GET] (findAllCats)
*/
However, the route parameter will be finally chosen if provided.
@Controller('cats')
export class CatsController {
@LoggedRoute('findAllCats') // 'findAllCats' route
@Get('/findall')
public async findAllCats() {
// ...
}
}
/*
LoggedRoute Log Message:
route
vvvvvvvvvvv
... HIT HTTP CatsController::findAllCats[GET] (findAllCats)
...
route
vvvvvvvvvvv
... RETURNED HTTP CatsController::findAllCats[GET] (findAllCats)
*/
Even the route in the log is set via route parameter of LoggedRoute, it still uses route value from nestjs decorators like Get or Post for HTTP request.
In the example before, the 'findAllCats' route is provided in the parameter of LoggedRoute, it still takes request from /cats/findall.