I am looking to improve the functionality of an Interceptor within my LoopBack4 application. Currently, the Interceptor simply displays the start and end of a controller method call on the command line, as outlined in this documentation.
Here is how my Log-Interceptor currently looks:
export const Log: Interceptor = async (invocationCtx, next) => {
// Wait until the interceptor/method chain returns
const req = await invocationCtx.get(RestBindings.Http.REQUEST);
try
{
const stackinfo = 'Class: ' + invocationCtx.targetClass.name + ' | Method: ' + invocationCtx.methodName + " | Request IPs: " + req.ips.concat(', ');
logger.trace('Starting - ' + stackinfo);
const result = await next();
const res = await invocationCtx.get(RestBindings.Http.RESPONSE);
logger.trace('Ending - ' + stackinfo + ' | Response Status Code: ' + res.statusCode);
return result;
}
catch (e)
{
logger.error(e);
throw e;
}
};
Now I would like to enhance this Interceptor to also log additional statistics data into my MySQL-Datasource. My challenge lies in figuring out how to access the repository within the interceptor. Should I inject the Repository, and if so, what is the correct way to do that? Alternatively, are there better methods to achieve this goal?