Instead of adding a logger to all your services, you could potentially use an interceptor as an in between for all your services.
logger.interceptor.ts
import {Injectable} from '@angular/core';
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
import {Observable} from 'rxjs';
@Injectable()
export class LoggerInterceptor implements HttpInterceptor {
constructor() {
}
/**
* @param {HttpRequest<any>} req
* @param {HttpHandler} next
* @returns {Observable<HttpEvent<any>>}
*/
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log(req);
}
}
Then following this example you can use the useFacotry
(or possibly even the useClass
in the same way) in your app.module.ts to switch between what you import. This is untested code so you may have to play with it a little to get it to work.
app.module.ts
import {LoggerInterceptor} from './path/to/realm.interceptor';
import {environment} from './environments/environment';
...
providers: [
{
provide: HTTP_INTERCEPTORS,
multi: true,
useClass: () => {
if (!environment.production) {
return new loggerService();
}
}
},
]