logger.service.ts
angular/examples/week11-12/logger.service.ts
// @ts-nocheck
import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({ providedIn: 'root' })
export class LoggerService {
private http = inject(HttpClient);
private endpoint = '/api/logs';
info(message: string, context?: Record<string, unknown>): void {
this.dispatch('info', message, context);
}
warn(message: string, context?: Record<string, unknown>): void {
this.dispatch('warn', message, context);
}
error(message: string, context?: Record<string, unknown>): void {
this.dispatch('error', message, context);
}
private dispatch(level: 'info' | 'warn' | 'error', message: string, context?: Record<string, unknown>) {
if (typeof window === 'undefined') {
console[level](`[server] ${message}`, context);
return;
}
void this.http.post(this.endpoint, {
level,
message,
context,
timestamp: new Date().toISOString(),
}).toPromise();
}
}
Articles liés
server.js
server.js — javascript source code from the angular learning materials (angular/comprehensive-demo/server/server.js).
Lire l'article →app.component.ts
app.component.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.component.ts).
Lire l'article →app.config.ts
app.config.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.config.ts).
Lire l'article →app.routes.ts
app.routes.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.routes.ts).
Lire l'article →auth.guard.ts
auth.guard.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/core/guards/auth.guard.ts).
Lire l'article →role.guard.ts
role.guard.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/core/guards/role.guard.ts).
Lire l'article →