Add Tutorial

p-sw 2025-03-26 22:54:57 +00:00
parent e2c220d9cf
commit 4848425075

89
Tutorial.md Normal file

@ -0,0 +1,89 @@
In this page, we'll provide a guide to learn how to use NestLogged package.
# Method Logging
## Basic
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.
```ts
// 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] ID=[...] | CatsController.findAllCats: HIT ENDPOINT CatsController.findAllCats (CatsController::/[GET])
...
[Nest] 4208 - 01/12/2024, 5:16:02 PM LOG [CatsController] ID=[...] | CatsController.findAllCats: RETURNED ENDPOINT CatsController.findAllCats (CatsController::/[GET])
*/
// 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] CatsService.findAllCats: HIT FUNCTION CatsService.findAllCats
...
[Nest] 23564 - 01/12/2024, 5:17:40 PM LOG [CatsService] CatsService.findAllCats: RETURNED FUNCTION CatsService.findAllCats
*/
```
## Decorator Options
You can put option object to set decorator's settings.
Both decorator takes some type of option. You can see the definition of option and default values in below.
```ts
interface OverrideBuildOptions {
callLogLevel: LogLevel | 'skip';
returnLogLevel: LogLevel | 'skip';
errorLogLevel: LogLevel | 'skip';
/** @deprecated use `callLogLevel: 'skip'` instead */
skipCallLog: boolean;
/** @deprecated use `returnLogLevel: 'skip'` instead */
skipReturnLog: boolean;
/** @deprecated use `errorLogLevel: 'skip'` instead */
skipErrorLog: boolean;
}
const defaultOverrideBuildOptions: OverrideBuildOptions = {
callLogLevel: 'log',
returnLogLevel: 'log',
errorLogLevel: 'error',
skipCallLog: false,
skipReturnLog: false,
skipErrorLog: false,
}
export function LoggedFunction<F extends Array<any>, R>(
options?: Partial<OverrideBuildOptions>
) {
...
}
export function LoggedRoute<F extends Array<any>, R>(
route?: string,
options?: Partial<OverrideBuildOptions>
) {
...
}