@naturalcycles/js-lib
Version:
Standard library for universal (browser + Node.js) javascript
62 lines (61 loc) • 1.62 kB
TypeScript
import type { CommonLogger } from '../log/commonLogger.js';
/**
* $r - result
*
* @returns array of tokens that will be `.filter(Boolean).join(' ')`
*/
type LogResultFn = (r: any) => any[];
export interface LogMethodOptions {
/**
* Log "moving average" elapsed time for up to `avg` last method calls
*/
avg?: number;
/**
* Defaults to true.
* Set to false to skip logging method arguments
*/
logArgs?: boolean;
/**
* Defaults to true.
* Set to false to skip logging result length when result is an array.
*/
logResultLength?: boolean;
/**
* Also log on method start.
* Example:
*
* >> syncMethodSuccess()
*/
logStart?: boolean;
/**
* Log method result as is (stringified).
* Example:
*
* * << syncMethodSuccess() took 124 ms result: 'SomeString as result'
*/
logResult?: boolean;
/**
* Log method result via provided function that takes "result as is" as first argument and should return a String.
* Overrides `logResult`.
*/
logResultFn?: LogResultFn;
/**
* Defaults to `console`
*/
logger?: CommonLogger;
}
/**
* Console-logs when method had started, when it finished, time taken and if error happened.
* Supports both sync and async methods.
* Awaits if method returns a Promise.
*
* @example output:
*
* >> syncMethodSuccess()
* << syncMethodSuccess() took 124 ms
*
* >> asyncMethod()
* << asyncMethodThrow() took 10 ms ERROR: MyError
*/
export declare function _LogMethod(opt?: LogMethodOptions): MethodDecorator;
export {};