-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from gsainfoteam/siwonpada/issue29
- Loading branch information
Showing
37 changed files
with
177 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
"gistory", | ||
"gsainfoteam", | ||
"infoteam", | ||
"LOGGABLE", | ||
"metatype", | ||
"nestjs", | ||
"noreply", | ||
"openai", | ||
|
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,4 @@ | ||
import { SetMetadata } from '@nestjs/common'; | ||
|
||
export const LOGGABLE = Symbol('LOGGABLE'); | ||
export const Loggable = () => SetMetadata(LOGGABLE, true); |
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,2 @@ | ||
export * from './logger.module'; | ||
export * from './logger.service'; |
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,48 @@ | ||
import { Injectable, Logger, OnModuleInit } from '@nestjs/common'; | ||
import { DiscoveryService, MetadataScanner } from '@nestjs/core'; | ||
import { LOGGABLE } from './decorator/loggable'; | ||
|
||
@Injectable() | ||
export class LoggerDecoratorRegister implements OnModuleInit { | ||
constructor( | ||
private readonly discoveryService: DiscoveryService, | ||
private readonly metadataScanner: MetadataScanner, | ||
) {} | ||
|
||
onModuleInit() { | ||
return this.discoveryService | ||
.getProviders() | ||
.filter((wrapper) => wrapper.isDependencyTreeStatic()) | ||
.filter(({ metatype, instance }) => { | ||
if (!instance || !metatype) { | ||
return false; | ||
} | ||
return Reflect.getMetadata(LOGGABLE, metatype); | ||
}) | ||
.forEach(({ instance }) => { | ||
this.metadataScanner | ||
.getAllMethodNames(instance) | ||
.forEach((methodName) => { | ||
const originalMethod = instance[methodName]; | ||
if (typeof originalMethod !== 'function') { | ||
return; | ||
} | ||
const logger = new Logger(instance.constructor.name); | ||
instance[methodName] = function (...args: any[]) { | ||
logger.log(`Before ${methodName}`); | ||
const now = Date.now(); | ||
const result = originalMethod.apply(this, args); | ||
if (result instanceof Promise) { | ||
return result.then((resolvedResult) => { | ||
logger.log(`After ${methodName} +${Date.now() - now}ms`); | ||
return resolvedResult; | ||
}); | ||
} else { | ||
logger.log(`After ${methodName} +${Date.now() - now}ms`); | ||
return result; | ||
} | ||
}; | ||
}); | ||
}); | ||
} | ||
} |
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,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { LoggerService } from './logger.service'; | ||
import { DiscoveryModule } from '@nestjs/core'; | ||
import { LoggerDecoratorRegister } from './logger.decorator.register'; | ||
|
||
@Module({ | ||
imports: [DiscoveryModule], | ||
providers: [LoggerService, LoggerDecoratorRegister], | ||
exports: [LoggerService], | ||
}) | ||
export class LoggerModule {} |
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,4 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class LoggerService {} |
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,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"declaration": true, | ||
"outDir": "../../dist/libs/logger" | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules", "dist", "test", "**/*spec.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
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
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
Oops, something went wrong.