Skip to content

Instantly share code, notes, and snippets.

@tioback
Created January 21, 2019 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tioback/77ee9b0c0496677f388ab4fff3128ac2 to your computer and use it in GitHub Desktop.
Save tioback/77ee9b0c0496677f388ab4fff3128ac2 to your computer and use it in GitHub Desktop.
App Logger usando funções anônimas ao invés de arrow functions
import * as _ from 'lodash';
import { FunctionLoggerOptions } from './interfaces';
import { defaultFunctionOptions } from './default-options';
import { logMessage } from './messages.helper';
export const logger = function (options = defaultFunctionOptions): Function {
return function(target: any, methodName: string, descriptor: any) {
if (descriptor === undefined) {
descriptor = Object.getOwnPropertyDescriptor(target, methodName);
}
const originalMethod = descriptor.value;
// descriptor.value = getMonkeyPatchMethod(target.name, originalMethod, methodName, options);
descriptor.value = getMonkeyPatchMethod(target.constructor.name, originalMethod, methodName, options, target);
descriptor.value.__loggerMonkeyPatchCompleted = true;
return descriptor;
};
};
const disableMethodLogger = function(): Function {
return function (target: any, methodName: string, descriptor: any) {
if (descriptor === undefined) {
descriptor = Object.getOwnPropertyDescriptor(target, methodName);
}
const originalMethod = descriptor.value;
originalMethod.__loggerMonkeyPatchCompleted = true;
return descriptor;
};
};
export const getMonkeyPatchMethod = function (className: string, method: Function, methodName: string, options: FunctionLoggerOptions, target?: any): Function {
return async function(...args: any[]) {
const init = new Date().getTime();
// logMessage(className, true, this, methodName, method, args, options, target);
logMessage(className, true, this, methodName, method, args, options);
try {
console.log('O que é o this?', this);
// return method.apply(this, args);
console.log('antes res');
console.log('method: ');
console.log(method);
const res = await method.apply(this, args);
console.log('res from inside getMonkeyPatchMethod', res);
return res;
// return method.apply(this, args);
} catch (e) {
// logMessage(className, false, this, methodName, method, args, options, init, target);
logMessage(className, false, this, methodName, method, args, options, init);
throw e;
} finally {
// logMessage(className, false, this, methodName, method, args, options, init);
logMessage(className, false, this, methodName, method, args, options, init);
}
}
};
export function Logger(options = defaultFunctionOptions): Function {
return logger(options);
}
export function LoggerWithoutArgs(options = defaultFunctionOptions): Function {
options = _.extend({}, options, {
withArgs: false
});
return Logger(options);
}
export function DisableMethodLogger(): Function {
return disableMethodLogger();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment