Currently I'm using the NestJs default logging and come up with something like this
@Controller('users')
export class UsersController {
private logger: Logger = new Logger(UsersController.name, true);
@Get()
public getAllUsers(): any[] {
this.logger.debug('Getting all users', this.getAllUsers.name);
return [];
}
}
The log message will show up
[Nest] 2926 - 01/05/2020, 7:39:19 PM [getAllUsers] Getting all users +2094ms
It would be cool to set the context parameter automatically to only need to write
this.logger.debugFunction('Getting all users');
because when logging controller, service and repository functions things could get a bit messy. So from the docs
https://docs.nestjs.com/techniques/logger#extend-built-in-logger
it's possible to extend the default logging behaviour. Taking the example from the docs I would come up with
export class MyLogger extends Logger {
debugFunction(message: string) {
super.debug(message, 'this.caller.name');
}
}
This has been asked before here
How do you find out the caller function in JavaScript?
but this.debugFunction.caller
doesn't seem to be a good way because the docs clearly say
Is there a way I can determine the context (caller) automatically in a standard way for production code?