You can use @Returns
decorator to log returned value similar to parameter logging.
import { Injectable } from '@nestjs/common';
import { LoggedFunction, Logged, Returns } from 'nestlogged';
@Injectable()
class UserService {
@LoggedFunction
@Returns()
async getUser(
@Logged("userId")
userId: string
) {
return {
body: {
user: {
id: userId,
name: "tester",
},
secret: "supersecret",
},
};
}
}
/*
example: userService.getUser("user-asdf")
[UserService] CALL getUser WITH userId=user-asdf
[UserService] RETURNED getUser WITH {"body":{"user":{"id":"user-asdf","name":"tester"},"secret":"supersecret"}}
*/
You can put name to returned value. Name of the returned value can be set by giving string as the parameter of decorator.
import { Injectable } from '@nestjs/common';
import { LoggedFunction, Logged, Returns } from 'nestlogged';
@Injectable()
class UserService {
@LoggedFunction
@Returns("Result")
async getUser(
@Logged("userId")
userId: string
) {
return {
body: {
user: {
id: userId,
name: "tester",
},
secret: "supersecret",
},
};
}
}
/*
example: userService.getUser("user-asdf")
[UserService] CALL getUser WITH userId=user-asdf
[UserService] RETURNED getUser WITH Result={"body":{"user":{"id":"user-asdf","name":"tester"},"secret":"supersecret"}}
*/
Also, you can set the property of the returned value to log with name. This can be set by giving object as the parameter of decorator.
import { Injectable } from '@nestjs/common';
import { LoggedFunction, Logged, Returns } from 'nestlogged';
@Injectable()
class UserService {
@LoggedFunction
@Returns({ userId: 'body.user.id', userName: 'body.user.name' })
async getUser(
@Logged("userId")
userId: string
) {
return {
body: {
user: {
id: userId,
name: "tester",
},
secret: "supersecret",
},
};
}
}
/*
example: userService.getUser("user-asdf")
[UserService] CALL getUser WITH userId=user-asdf
[UserService] RETURNED getUser WITH userId=user-asdf, userName=tester
*/
When property is not found, that property will not be included in the log.
import { Injectable } from '@nestjs/common';
import { LoggedFunction, Logged, Returns } from 'nestlogged';
@Injectable()
class UserService {
@LoggedFunction
@Returns({ httpCode: 'http.status', userName: 'body.user.name' }) // httpCode not found.
async getUser(
@Logged("userId")
userId: string
) {
return {
body: {
user: {
id: userId,
name: "tester",
},
secret: "supersecret",
},
};
}
}
/*
example: userService.getUser("user-asdf")
[UserService] CALL getUser WITH userId=user-asdf
[UserService] RETURNED getUser WITH userName=tester // httpCode not included.
*/