All files / src/utils logMethod.ts

100% Statements 15/15
62.5% Branches 5/8
100% Functions 5/5
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31  1x 48x         48x 48x 77x 77x           77x 77x 77x 77x           77x 77x   19x    
import { Logger } from '../services/logger.services';
export const log = (propertyName?: string, message?: string) => {
  return function (
    target: any,
    key: string | symbol,
    propertyDescriptor: PropertyDescriptor,
  ) {
    const method = propertyDescriptor.value;
    propertyDescriptor.value = async function (...args: any[]) {
      const params = args.map(a => JSON.stringify(a)).join();
      const logData = {
        methodName: key,
        propertyName: propertyName ? propertyName : 'Input Parameter',
        propertyValue: isJSON(params) ? JSON.parse(params) : params,
        message: message ? message : '',
      };
      const logger: Logger = Reflect.getMetadata('logger', target);
      logger && logger.verbose(logData);
      const result = method.apply(this, args, logData);
      return result;
    };
  };
};
 
function isJSON(params: any) {
  try {
    return JSON.parse(params);
  } catch (e) {
    return false;
  }
}