Skip to content

Commit 58eff46

Browse files
author
liujia02
committed
add abstract
1 parent 4e78837 commit 58eff46

File tree

7 files changed

+755
-739
lines changed

7 files changed

+755
-739
lines changed

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,30 @@
1212
"url": "git+https://github.com/marsprince/nest-log4js"
1313
},
1414
"dependencies": {
15+
"circular-json-es6": "^2.0.2",
1516
"log4js": "^3.0.5"
1617
},
1718
"devDependencies": {
18-
"reflect-metadata": "^0.1.12",
19-
"rxjs": "^6.0.0",
20-
"typescript": "^2.6.2",
2119
"@nestjs/common": "^5.0.0",
2220
"@nestjs/core": "^5.0.0",
23-
"fastify-formbody": "^2.0.0",
2421
"@nestjs/testing": "^5.0.0",
2522
"@types/express": "^4.0.39",
2623
"@types/jest": "^21.1.8",
2724
"@types/node": "^9.3.0",
2825
"@types/supertest": "^2.0.4",
26+
"fastify-formbody": "^2.0.0",
2927
"jest": "^21.2.1",
3028
"nodemon": "^1.14.1",
3129
"prettier": "^1.11.1",
30+
"reflect-metadata": "^0.1.12",
31+
"rxjs": "^6.0.0",
3232
"supertest": "^3.0.0",
3333
"ts-jest": "^21.2.4",
3434
"ts-loader": "^4.1.0",
3535
"ts-node": "^4.1.0",
3636
"tsconfig-paths": "^3.1.1",
37-
"tslint": "5.3.2"
37+
"tslint": "5.3.2",
38+
"typescript": "^2.6.2"
3839
},
3940
"jest": {
4041
"moduleFileExtensions": [

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './log4js.constant';
22
export * from './log4js.filter';
3+
export * from './log4js.interceptor.abstract';
34
export * from './log4js.interceptor';
45
export * from './log4js.module';
56
export * from './log4js.provider';

src/log4js.interceptor.abstract.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { NestInterceptor, ExecutionContext } from '@nestjs/common';
2+
import { Observable } from 'rxjs';
3+
import { tap } from 'rxjs/operators';
4+
import { Logger } from 'log4js';
5+
6+
export abstract class Log4jsInterceptorAbstract implements NestInterceptor {
7+
protected constructor(
8+
protected requestLogger: Logger,
9+
protected responseLogger: Logger,
10+
) {
11+
}
12+
13+
intercept(
14+
context: ExecutionContext,
15+
call$: Observable<any>,
16+
): Observable<any> {
17+
const httpRequest = context.switchToHttp().getRequest();
18+
this.requestLogger.info(this.requestFormat(httpRequest));
19+
return call$.pipe(
20+
tap(httpResponse => {
21+
this.responseLogger.info(this.responseFormat(httpResponse));
22+
}),
23+
);
24+
}
25+
26+
abstract requestFormat<T>(httpRequest: T): string;
27+
28+
abstract responseFormat<T>(httpResponse: T): string;
29+
}

src/log4js.interceptor.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
1-
import { Injectable, NestInterceptor, ExecutionContext, Inject } from '@nestjs/common';
1+
import { Injectable, ExecutionContext, Inject } from '@nestjs/common';
22
import { Observable } from 'rxjs';
3-
import { tap, catchError } from 'rxjs/operators';
43
import { LOG4JS_REQUEST_LOGGER, LOG4JS_RESPONSE_LOGGER } from './log4js.constant';
54
import { Logger } from 'log4js';
5+
import { Log4jsInterceptorAbstract } from './log4js.interceptor.abstract';
6+
import { stringify } from 'circular-json-es6';
67

78
@Injectable()
8-
export class Log4jsInterceptor implements NestInterceptor {
9+
export class Log4jsInterceptor extends Log4jsInterceptorAbstract {
910
constructor(
10-
@Inject(LOG4JS_REQUEST_LOGGER) private requestLogger: Logger,
11-
@Inject(LOG4JS_RESPONSE_LOGGER) private responseLogger: Logger,
11+
@Inject(LOG4JS_REQUEST_LOGGER) protected requestLogger: Logger,
12+
@Inject(LOG4JS_RESPONSE_LOGGER) protected responseLogger: Logger,
1213
) {
14+
super(responseLogger, responseLogger);
1315
}
1416

1517
intercept(
1618
context: ExecutionContext,
1719
call$: Observable<any>,
1820
): Observable<any> {
19-
const httpRequest = context.switchToHttp().getRequest();
20-
this.requestLogger.info(httpRequest);
21-
return call$.pipe(
22-
tap(httpResponse => {
23-
this.responseLogger.info(httpResponse);
24-
}),
25-
);
21+
return super.intercept(context, call$);
22+
}
23+
24+
requestFormat(httpRequest: any): string {
25+
return stringify({
26+
url: httpRequest.url,
27+
method: httpRequest.method,
28+
params: httpRequest.params,
29+
query: httpRequest.query,
30+
body: httpRequest.body,
31+
httpVersion: httpRequest.httpVersion,
32+
headers: httpRequest.headers,
33+
route: httpRequest.route,
34+
});
35+
}
36+
37+
responseFormat(httpResponse: any): string {
38+
return stringify(httpResponse);
2639
}
2740
}

test/app.e2e-spec.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

test/jest-e2e.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)