Skip to content

Commit

Permalink
chore: provide log interceptor logging REST call durations
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi committed Mar 16, 2020
1 parent ccffb8a commit 7e35007
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/app/app.server.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { join } from 'path';
import { Observable, Observer } from 'rxjs';

import { DISPLAY_VERSION } from 'ish-core/configurations/state-keys';
import { UniversalLogInterceptor } from 'ish-core/interceptors/universal-log.interceptor';
import { UniversalMockInterceptor } from 'ish-core/interceptors/universal-mock.interceptor';
import { coreReducers } from 'ish-core/store/core-store.module';

Expand Down Expand Up @@ -57,7 +58,10 @@ export function translateLoaderFactory() {
},
}),
],
providers: [{ provide: HTTP_INTERCEPTORS, useClass: UniversalMockInterceptor, multi: true }],
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: UniversalMockInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: UniversalLogInterceptor, multi: true },
],
bootstrap: [AppComponent],
})
export class AppServerModule {
Expand Down
24 changes: 24 additions & 0 deletions src/app/core/interceptors/universal-log.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

export class UniversalLogInterceptor implements HttpInterceptor {
// tslint:disable-next-line: no-any
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (!process.env.LOGGING) {
return next.handle(req);
}
const start = new Date().getTime();
return next.handle(req).pipe(
tap(res => {
if (res instanceof HttpResponse) {
// tslint:disable-next-line: no-console
console.log(
`${req.method} ${req.urlWithParams} ${res.status} ${JSON.stringify(res.body).length *
2} - ${new Date().getTime() - start} ms`
);
}
})
);
}
}

0 comments on commit 7e35007

Please sign in to comment.