Introduction
Last updated
Last updated
NestLogged is a logging decorator set for NestJS. It provides simple and flexible ways to remove loggings from essential code in NestJS, so developer can focus on the real code.
You can get logs with just a few simple decorators attached to the class, function, parameter.
NestLogged decorator have features like initializes logger, add scope to logger, print logs on function call and return, log parameter values, etc.
$ npm install nestlogged
Or
$ yarn add nestlogged
Usually we do:
cats.controller.ts
@Controller('cats')
export class CatsController {
private readonly logger = new Logger(CatsController.name);
constructor(private catsService: CatsService) {}
@Get(':name')
findCatByName(
@Param('name')
name: string
) {
this.logger.log(`/cats/:name called with name=${name}`);
const cat = this.catsService.findCatByName(name);
if (!cat) {
this.logger.error(`There is no cat named ${name}..`);
throw new NotFoundException('cat_not_found');
}
this.logger.log(`Found cat ${cat}!!`)
return cat;
}
}
cats.service.ts
@Injectable()
export class CatsService {
private readonly logger = new Logger(CatsService.name);
private readonly cats = [
{
name: "Adela"
},
{
name: "Baba"
}
];
findCatByName(name: string): string {
this.logger.log(`findCatByName called with name=${name}`)
const foundCat = this.cats.filter((cat) => cat.name === name);
this.logger.log(`found ${foundCat.length} cats`)
if (!foundCat.length) return null;
this.logger.log(`returning cat ${foundCat[0].name}`)
return foundCat[0].name
}
}
Console (success with name=Baba)
[Nest] 22072 - 01/10/2024, 2:26:02 AM LOG [CatsController] /cats/:name called with name=Baba
[Nest] 22072 - 01/10/2024, 2:26:02 AM LOG [CatsService] findCatByName called with name=Baba
[Nest] 22072 - 01/10/2024, 2:26:02 AM LOG [CatsService] found 1 cats
[Nest] 22072 - 01/10/2024, 2:26:02 AM LOG [CatsService] returning cat Baba
[Nest] 22072 - 01/10/2024, 2:26:02 AM LOG [CatsController] Found cat Baba!!
Console (fail with name=baba)
[Nest] 13064 - 01/10/2024, 2:27:29 AM LOG [CatsController] /cats/:name called with name=baba
[Nest] 13064 - 01/10/2024, 2:27:29 AM LOG [CatsService] findCatByName called with name=baba
[Nest] 13064 - 01/10/2024, 2:27:29 AM LOG [CatsService] found 0 cats
[Nest] 13064 - 01/10/2024, 2:27:29 AM ERROR [CatsController] There is no cat named baba..
Applied NestLogged decorators:
cats.controller.ts
@LoggedController('cats')
export class CatsController {
constructor(private catsService: CatsService) {}
@Get(':name')
findCatByName(
@LoggedParam('name')('name') name: string,
@InjectLogger logger: ScopedLogger,
) {
const cat = this.catsService.findCatByName(name, logger);
if (!cat) {
throw new NotFoundException('cat_not_found');
}
logger.log(`Found cat ${cat}!!`)
return cat;
}
}
cats.service.ts
@LoggedInjectable()
export class CatsService {
private readonly cats = [
{
name: "Adela"
},
{
name: "Baba"
}
];
@Returns('name')
findCatByName(
@Logged('name') name: string,
@InjectLogger logger: ScopedLogger,
): string {
const foundCat = this.cats.filter((cat) => cat.name === name);
this.logger.log(`found ${foundCat.length} cats`)
if (!foundCat.length) return null;
return foundCat[0].name
}
}
Console (success with name=Baba)
[Nest] 13784 - 01/10/2024, 2:14:54 AM LOG [CatsController] findCatByName(w4ZOccMCT6qaQRFJig82nw/0000000000): HIT HTTP CatsController:::name[GET] (findCatByName) WITH name=Baba
[Nest] 13784 - 01/10/2024, 2:14:54 AM LOG [CatsController] findCatByName(w4ZOccMCT6qaQRFJig82nw/0000000000): -> findCatByName: CALL findCatByName WITH name=Baba
[Nest] 13784 - 01/10/2024, 2:14:54 AM LOG [CatsController] findCatByName(w4ZOccMCT6qaQRFJig82nw/0000000000): -> findCatByName: found 1 cats
[Nest] 13784 - 01/10/2024, 2:14:54 AM LOG [CatsController] findCatByName(w4ZOccMCT6qaQRFJig82nw/0000000000): -> findCatByName: RETURNED findCatByName Baba
[Nest] 13784 - 01/10/2024, 2:14:54 AM LOG [CatsController] findCatByName(w4ZOccMCT6qaQRFJig82nw/0000000000): Found cat Baba!!
[Nest] 13784 - 01/10/2024, 2:14:54 AM LOG [CatsController] findCatByName(w4ZOccMCT6qaQRFJig82nw/0000000000): RETURNED HTTP CatsController:::name[GET] (findCatByName)
Console (fail with nam=baba)
[Nest] 20668 - 01/10/2024, 2:28:42 AM LOG [CatsController] findCatByName(sIb8e7UUTyubQecsS+rHyw/0000000001): HIT HTTP CatsController:::name[GET] (findCatByName) WITH name=baba
[Nest] 20668 - 01/10/2024, 2:28:42 AM LOG [CatsController] findCatByName(sIb8e7UUTyubQecsS+rHyw/0000000001): -> findCatByName: CALL findCatByName WITH name=baba
[Nest] 20668 - 01/10/2024, 2:28:42 AM LOG [CatsController] findCatByName(sIb8e7UUTyubQecsS+rHyw/0000000001): -> findCatByName: found 0 cats
[Nest] 20668 - 01/10/2024, 2:28:42 AM LOG [CatsController] findCatByName(sIb8e7UUTyubQecsS+rHyw/0000000001): -> findCatByName: RETURNED findCatByName null
[Nest] 20668 - 01/10/2024, 2:28:42 AM ERROR [CatsController] findCatByName(sIb8e7UUTyubQecsS+rHyw/0000000001): WHILE HTTP CatsController:::name[GET] (findCatByName) ERROR NotFoundException: cat_not_found