generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ORV2-1806 Resolve LogMethodExecution Decorator for non Async Met…
…hods (#963)
- Loading branch information
Showing
27 changed files
with
248 additions
and
144 deletions.
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
43 changes: 43 additions & 0 deletions
43
dops/src/decorator/log-async-method-execution.decorator.ts
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,43 @@ | ||
import { Logger } from '@nestjs/common'; | ||
|
||
export function LogAsyncMethodExecution(logMethodOptions?: { | ||
printMemoryStats: boolean; | ||
}) { | ||
return function ( | ||
target: object, | ||
propertyKey: string, | ||
descriptor: PropertyDescriptor, | ||
) { | ||
let memoryUsage = ''; | ||
/* eslint-disable */ | ||
const logger = new Logger(target.constructor.name); | ||
const originalMethod = descriptor.value; | ||
descriptor.value = async function (...args: any[]) { | ||
if (logMethodOptions?.printMemoryStats && | ||
process.env.DOPS_API_LOG_LEVEL === 'debug') { | ||
const memoryStats = process.memoryUsage(); | ||
memoryUsage = `, Memory usage: ${JSON.stringify(memoryStats)}`; | ||
} | ||
logger.debug( | ||
`>> Entering ${target.constructor.name}.${propertyKey} method${memoryUsage}`, | ||
); | ||
|
||
const start = performance.now(); | ||
const result = await originalMethod.apply(this, args); | ||
const end = performance.now(); | ||
const executionTime = end - start; | ||
if (logMethodOptions?.printMemoryStats && | ||
process.env.DOPS_API_LOG_LEVEL === 'debug') { | ||
const memoryStats = process.memoryUsage(); | ||
memoryUsage = `, Memory usage: ${JSON.stringify(memoryStats)}`; | ||
} | ||
logger.debug( | ||
`<< Exiting ${target.constructor.name}.${propertyKey} method, execution time: ${executionTime}ms${memoryUsage}`, | ||
); | ||
return result; | ||
}; | ||
/* eslint-enable */ | ||
|
||
return descriptor; | ||
}; | ||
} |
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
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
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
47 changes: 47 additions & 0 deletions
47
vehicles/src/common/decorator/log-async-method-execution.decorator.ts
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,47 @@ | ||
import { Logger } from '@nestjs/common'; | ||
|
||
export function LogAsyncMethodExecution(logMethodOptions?: { | ||
printMemoryStats: boolean; | ||
}) { | ||
return function ( | ||
target: object, | ||
propertyKey: string, | ||
descriptor: PropertyDescriptor, | ||
) { | ||
let memoryUsage = ''; | ||
/* eslint-disable */ | ||
const logger = new Logger(target.constructor.name); | ||
const originalMethod = descriptor.value; | ||
descriptor.value = async function (...args: any[]) { | ||
if ( | ||
logMethodOptions?.printMemoryStats && | ||
process.env.VEHICLES_API_LOG_LEVEL === 'debug' | ||
) { | ||
const memoryStats = process.memoryUsage(); | ||
memoryUsage = `, Memory usage: ${JSON.stringify(memoryStats)}`; | ||
} | ||
logger.debug( | ||
`>> Entering ${target.constructor.name}.${propertyKey} method${memoryUsage}`, | ||
); | ||
|
||
const start = performance.now(); | ||
const result = await originalMethod.apply(this, args); | ||
const end = performance.now(); | ||
const executionTime = end - start; | ||
if ( | ||
logMethodOptions?.printMemoryStats && | ||
process.env.VEHICLES_API_LOG_LEVEL === 'debug' | ||
) { | ||
const memoryStats = process.memoryUsage(); | ||
memoryUsage = `, Memory usage: ${JSON.stringify(memoryStats)}`; | ||
} | ||
logger.debug( | ||
`<< Exiting ${target.constructor.name}.${propertyKey} method, execution time: ${executionTime}ms${memoryUsage}`, | ||
); | ||
return result; | ||
}; | ||
/* eslint-enable */ | ||
|
||
return descriptor; | ||
}; | ||
} |
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
Oops, something went wrong.