-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(decorators): add KbMeasure decorator
this will wrap a method with a winston `startTimer()` and will inform how long the function ran. Added an example on the depricated API for now
- Loading branch information
1 parent
6af0bdd
commit 0049de8
Showing
3 changed files
with
35 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { WinstonLogger } from '@kibibit/nestjs-winston'; | ||
|
||
const logger = new WinstonLogger('KbMeasure'); | ||
|
||
export const KbMeasure = (controlerName?: string) => ( | ||
target: unknown, | ||
propertyKey: string, | ||
descriptor: PropertyDescriptor | ||
) => { | ||
const originalMethod = descriptor.value; | ||
|
||
descriptor.value = async function (...args) { | ||
logger.verbose(generateLogMessagge('START')); | ||
const start = logger.startTimer(); | ||
const result = await Promise.resolve(originalMethod.apply(this, args)); | ||
start.done({ | ||
level: 'verbose', | ||
message: generateLogMessagge('END') | ||
}); | ||
return result; | ||
|
||
function generateLogMessagge(msg: string) { | ||
return [ | ||
`${ controlerName ? controlerName + '.' : '' }${ originalMethod.name }`, | ||
`(${ args && args.length ? '...' : '' }) ${ msg }` | ||
].join(''); | ||
} | ||
}; | ||
|
||
return descriptor; | ||
}; |