I'd like to enhance an Interceptor in my LoopBack4 application, which currently simply prints me the start and end of a controller method call on the command line - as described here: https://loopback.io/doc/en/lb4/Interceptors.html
My Log-Interceptor looks like this:
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'd like to enhance this Interceptor to also log some statistics data into my MySQL-Datasource. My problem is, how can i access the repository inside the interceptor? Do i have to inject the Repository and if yes, how do i have to do that? Or is there a better way to achieve this?