UNPKG

@dvsa/appdev-api-common

Version:

Utils library for common API functionality

43 lines (42 loc) 1.74 kB
import type { Logger } from "@aws-lambda-powertools/logger"; /** * Decorator to log the duration of a method execution by passing in an accessor function to retrieve the logger. * @param getLogger - A function that takes the class instance and returns the logger instance. * @param {string} label - Optional label for the log message. * * Note: If you are already using a logger as a class property, you can use the `Timed` decorator instead. * * @constructor * * Example usage: * class MyClass { * @TimedWithAccessor(() => Container.get(LOGGER)) * async findAll() {} // The log name here will be [findAll] * } */ export declare function TimedWithAccessor<T extends { logger: Logger; }>(getLogger: (self: T) => Logger, label?: string): (_target: unknown, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor; /** * Decorator to log the duration of a method execution. * @param {string} label - Optional label for the log message. * * Note: This decorator can be used on any class method that has a `logger` property. * This method will throw an error if the `logger` property is not found on the class instance. * If you are not using logger as a class property, you can use `LogDurationWithAccessor` instead. * @constructor * * Example usage: * class MyClass { * private readonly logger = Container.get(Logger); * * @Timed() * myMethod() {} // The log name here will be [myMethod] * * @Timed('MyClass.myMethod') * myMethodWithOptName() {} // The log name here will be [MyClass.myMethod] * } */ export declare function Timed<T extends { logger: Logger; }>(label?: string): (_target: unknown, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;