Created
August 31, 2022 07:11
-
-
Save teomanofficial/30a28368c35a7fece39caf694e243eed to your computer and use it in GitHub Desktop.
Angular - Log request and response details to elastic
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
// 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