Я обнаружил, что через Interceptor мы можем создать журнал.
var uniqid = require('uniqid');
import { RestBindings } from '@loopback/rest';
import { Interceptor } from '@loopback/context';
log4js.configure({
appenders: { cheese: { type: 'dateFile', filename: 'cheese.log', pattern: '.yyyy-MM-dd-hh-mm', compress: true } },
categories: { default: { appenders: ['cheese'], level: 'debug' } }
});
const logger = log4js.getLogger(process.env.NODE_ENV);
logger.info("Application starts and running")
export const log: Interceptor = async (invocationCtx, next) => {
// Wait until the interceptor/method chain returns
const req = await invocationCtx.get(RestBindings.Http.REQUEST);
logger.info("Requestid - " + uniqid() + "| Request IP -" + req.ip);
try {
logger.info('Starting - Class-' + invocationCtx.targetClass.name + ' | Method-' + invocationCtx.methodName);
//logger.debug("Requestid - " + uniqid() + "| Request IP -" + req.ip);
const result = await next();
const res = await invocationCtx.get(RestBindings.Http.RESPONSE);
logger.info('Ending - Class-' + invocationCtx.targetClass.name + ' | Method-' + invocationCtx.methodName);
logger.info("Response Status Code - " + res.statusCode);
return result;
} catch (e) {
logger.error(e);
throw e;
}
};
В вашем классе добавьте объект-перехватчик с этим объектом журнала
Class:
import { intercept } from '@loopback/context';
import {Log} from './Log'
@intercept(log) // `log` is an interceptor function and above object
export class PingController {
//Your code
}