Compare commits

...

12 Commits

13 changed files with 177 additions and 16 deletions

Binary file not shown.

View File

@ -1,6 +1,6 @@
{
"name": "nestlogged-fastify",
"version": "3.3.1",
"version": "3.4.2",
"description": "A NestJS Logger Decorator Library With Fastify Support",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
@ -11,7 +11,7 @@
"@nestjs/common": "^11.0.12",
"fastify": "^5.2.2",
"hyperid": "^3.1.1",
"nestlogged": "^3.3.0",
"nestlogged": "^3.4.2",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
},

View File

@ -7,7 +7,9 @@ export {
LoggedQuery,
Logged,
Returns,
getRequestLogger
getRequestLogger,
Symbol_NestloggedScope,
Symbol_NestloggedScopeId
} from 'nestlogged';
export {
LoggedRoute,
@ -17,4 +19,5 @@ export {
LoggedGuard,
LoggedInterceptor,
LoggedMiddleware,
LoggedExceptionFilter,
} from './logged'

View File

@ -0,0 +1,75 @@
import { OverrideBuildOptions } from 'nestlogged/lib/logged/utils';
import { LoggedMetadata, nestLoggedMetadata } from 'nestlogged/lib/logged/metadata';
import { scopedLogger, returns, ReturnsReflectData } from 'nestlogged/lib/reflected';
import { overrideBuild } from '../override';
export function LoggedExceptionFilter<F extends Array<any>, R>(
options?: Partial<OverrideBuildOptions>,
) {
return (
_target: any,
key: string,
descriptor: TypedPropertyDescriptor<
(...args: F) => R
>,
) => {
const fn = descriptor.value;
if (!fn || typeof fn !== 'function') {
console.warn(
`LoggedExceptionFilter decorator applied to non-function property: ${key}`,
);
return;
}
const logMetadata: LoggedMetadata | undefined = Reflect.getOwnMetadata(
nestLoggedMetadata,
_target,
key,
);
if (logMetadata) {
// already applied, override instead
logMetadata.updateOption(options);
return;
}
const newMetadata = new LoggedMetadata(options);
const all = Reflect.getMetadataKeys(fn).map((k) => [
k,
Reflect.getMetadata(k, fn),
]);
const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata(
scopedLogger,
_target,
key,
);
const returnsData: ReturnsReflectData[] | true = Reflect.getOwnMetadata(
returns,
fn,
);
const overrideFunction = overrideBuild(
'exception',
fn,
_target,
{
scopedLoggerInjectableParam,
loggedParams: [],
},
_target.constructor.name,
returnsData,
newMetadata,
);
_target[key] = overrideFunction;
descriptor.value = overrideFunction;
Reflect.defineMetadata(nestLoggedMetadata, newMetadata, _target, key);
all.forEach(([k, v]) => {
Reflect.defineMetadata(k, v, _target[key]);
Reflect.defineMetadata(k, v, descriptor.value);
});
};
}

View File

@ -3,3 +3,4 @@ export { LoggedRoute } from './route';
export { LoggedGuard } from './guard';
export { LoggedInterceptor } from './interceptor';
export { LoggedMiddleware } from './middleware';
export { LoggedExceptionFilter } from './exception';

View File

@ -26,7 +26,7 @@ export function overrideBuild<F extends Array<any>, R>(
route: string,
): (...args: F) => R;
export function overrideBuild<F extends Array<any>, R>(
type: 'function' | 'guard' | 'interceptor' | 'middleware',
type: 'function' | 'guard' | 'interceptor' | 'middleware' | 'exception',
originalFunction: (...args: F) => R,
_target: any,
metadatas: FunctionMetadata,
@ -70,9 +70,9 @@ export function overrideBuild<F extends Array<any>, R>(
}
} else {
// special, can access to request object
if (type === 'guard' || type === 'interceptor') {
if (type === 'guard' || type === 'interceptor' || type === 'exception') {
// args[0] == ExecutionContext
const ctx = args[0] as ExecutionContext;
const ctx = args[type === 'exception' ? 1 : 0] as ExecutionContext;
if (ctx.getType() !== 'http') {
injectedLogger.error(
'Cannot inject logger: Request type is not http',

View File

@ -1,6 +1,6 @@
{
"name": "nestlogged",
"version": "3.3.1",
"version": "3.4.2",
"description": "A NestJS Logger Decorator Library",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",

View File

@ -6,6 +6,7 @@ export {
LoggedGuard,
LoggedInterceptor,
LoggedMiddleware,
LoggedExceptionFilter
} from './logged';
export { ScopedLogger, ConsoleLogger, ConsoleLoggerOptions } from './logger';
export {
@ -17,4 +18,4 @@ export {
Logged,
Returns,
} from './reflected';
export { getRequestLogger } from './utils';
export { getRequestLogger, NestloggedScope as Symbol_NestloggedScope, NestloggedScopeId as Symbol_NestloggedScopeId } from './utils';

View File

@ -0,0 +1,75 @@
import { OverrideBuildOptions } from '../utils';
import { LoggedMetadata, nestLoggedMetadata } from '../metadata';
import { scopedLogger, returns, ReturnsReflectData } from '../../reflected';
import { overrideBuild } from '../override';
export function LoggedExceptionFilter<F extends Array<any>, R>(
options?: Partial<OverrideBuildOptions>,
) {
return (
_target: any,
key: string,
descriptor: TypedPropertyDescriptor<
(...args: F) => R
>,
) => {
const fn = descriptor.value;
if (!fn || typeof fn !== 'function') {
console.warn(
`LoggedExceptionFilter decorator applied to non-function property: ${key}`,
);
return;
}
const logMetadata: LoggedMetadata | undefined = Reflect.getOwnMetadata(
nestLoggedMetadata,
_target,
key,
);
if (logMetadata) {
// already applied, override instead
logMetadata.updateOption(options);
return;
}
const newMetadata = new LoggedMetadata(options);
const all = Reflect.getMetadataKeys(fn).map((k) => [
k,
Reflect.getMetadata(k, fn),
]);
const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata(
scopedLogger,
_target,
key,
);
const returnsData: ReturnsReflectData[] | true = Reflect.getOwnMetadata(
returns,
fn,
);
const overrideFunction = overrideBuild(
'exception',
fn,
_target,
{
scopedLoggerInjectableParam,
loggedParams: [],
},
_target.constructor.name,
returnsData,
newMetadata,
);
_target[key] = overrideFunction;
descriptor.value = overrideFunction;
Reflect.defineMetadata(nestLoggedMetadata, newMetadata, _target, key);
all.forEach(([k, v]) => {
Reflect.defineMetadata(k, v, _target[key]);
Reflect.defineMetadata(k, v, descriptor.value);
});
};
}

View File

@ -3,3 +3,4 @@ export { LoggedRoute } from './route';
export { LoggedGuard } from './guard';
export { LoggedInterceptor } from './interceptor';
export { LoggedMiddleware } from './middleware';
export { LoggedExceptionFilter } from './exception';

View File

@ -1,4 +1,4 @@
import { Logger, ExecutionContext } from '@nestjs/common';
import { Logger, ExecutionContext, ArgumentsHost } from '@nestjs/common';
import { LoggedParamReflectData, ReturnsReflectData } from '../reflected';
import { LoggedMetadata } from './metadata';
import {
@ -26,7 +26,7 @@ export function overrideBuild<F extends Array<any>, R>(
route: string,
): (...args: F) => R;
export function overrideBuild<F extends Array<any>, R>(
type: 'function' | 'guard' | 'interceptor' | 'middleware',
type: 'function' | 'guard' | 'interceptor' | 'middleware' | 'exception',
originalFunction: (...args: F) => R,
_target: any,
metadatas: FunctionMetadata,
@ -70,9 +70,9 @@ export function overrideBuild<F extends Array<any>, R>(
}
} else {
// special, can access to request object
if (type === 'guard' || type === 'interceptor') {
if (type === 'guard' || type === 'interceptor' || type === 'exception') {
// args[0] == ExecutionContext
const ctx = args[0] as ExecutionContext;
const ctx = args[type === 'exception' ? 1 : 0] as ExecutionContext;
if (ctx.getType() !== 'http') {
injectedLogger.error(
'Cannot inject logger: Request type is not http',
@ -116,12 +116,15 @@ export function overrideBuild<F extends Array<any>, R>(
}
// If this is ExecutionContext based function (e.g. Guard, Interceptor) get Request from Context
if (type === 'guard' || type === 'interceptor' || type === 'middleware') {
if (type === 'guard' || type === 'interceptor') {
const context = args[0] as ExecutionContext;
if (context.getType() === 'http') {
const req = context.switchToHttp().getRequest();
route = req.url;
}
} else if (type === 'middleware') {
const req = args[0];
route = req.url;
}
// Start Log

View File

@ -32,7 +32,8 @@ export type BuildType =
| 'function'
| 'guard'
| 'interceptor'
| 'middleware';
| 'middleware'
| 'exception';
const callLogIdentifyMessageDictionary: Record<BuildType, string> = {
route: 'ENDPOINT',
@ -40,6 +41,7 @@ const callLogIdentifyMessageDictionary: Record<BuildType, string> = {
guard: 'GUARD',
interceptor: 'INTERCEPTOR',
middleware: 'MIDDLEWARE',
exception: 'EXCEPTION FILTER',
};
export function createCallLogIdentifyMessage(

View File

@ -643,7 +643,7 @@ __metadata:
"@types/node": "npm:^20.9.1"
fastify: "npm:^5.2.2"
hyperid: "npm:^3.1.1"
nestlogged: "npm:^3.3.0"
nestlogged: "npm:^3.4.2"
reflect-metadata: "npm:^0.1.13"
rxjs: "npm:^7.8.1"
typescript: "npm:^5.2.2"
@ -661,7 +661,7 @@ __metadata:
languageName: unknown
linkType: soft
"nestlogged@npm:^3.3.0, nestlogged@workspace:packages/nestlogged":
"nestlogged@npm:^3.4.2, nestlogged@workspace:packages/nestlogged":
version: 0.0.0-use.local
resolution: "nestlogged@workspace:packages/nestlogged"
dependencies: