Skip to content

Instantly share code, notes, and snippets.

@teomanofficial
Created August 31, 2022 07:11
Show Gist options
  • Save teomanofficial/30a28368c35a7fece39caf694e243eed to your computer and use it in GitHub Desktop.
Save teomanofficial/30a28368c35a7fece39caf694e243eed to your computer and use it in GitHub Desktop.
Angular - Log request and response details to elastic
// Imports..
@Injectable({ providedIn: 'root' })
export class ElasticLogInterceptor implements HttpInterceptor {
constructor(private logger: ElasticService) {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request)
.pipe(
tap(event => {
if (event instanceof HttpResponse) {
if (this.shouldLogRequest(request)) {
this.createLogRequestFromEvent(request, event);
}
}
}),
catchError(event => {
if (event instanceof HttpErrorResponse) {
if (this.shouldLogRequest(request)) {
this.createLogRequestFromEvent(request, event);
}
}
return throwError(event);
})
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment