All files / logging logger.service.ts

100% Statements 19/19
100% Branches 0/0
91.66% Functions 11/12
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 1004x 4x 4x                                   7x 7x       1x       3x       1x       1x                                     4x 12x               7x             2x             5x             2x             2x         4x  
import { LogDispatcher } from './log-dispatcher.service';
import { LogLevel } from './log-provider.interface';
import { Injectable, inject } from '@angular/core';
 
/**
 * Logger interface matching console API
 */
export interface Logger {
    debug(message: string, data?: unknown): void;
    info(message: string, data?: unknown): void;
    warn(message: string, data?: unknown): void;
    error(message: string, data?: unknown): void;
}
 
/**
 * Context-bound logger wrapper
 * Provides console-like API with automatic context prefix
 */
class ContextLogger implements Logger {
    constructor(
        private readonly loggerService: LoggerService,
        private readonly context: string
    ) {}
 
    debug(message: string, data?: unknown): void {
        this.loggerService.debug(message, data, this.context);
    }
 
    info(message: string, data?: unknown): void {
        this.loggerService.info(message, data, this.context);
    }
 
    warn(message: string, data?: unknown): void {
        this.loggerService.warn(message, data, this.context);
    }
 
    error(message: string, data?: unknown): void {
        this.loggerService.error(message, data, this.context);
    }
}
 
/**
 * Centralized logging service
 *
 * Delegates all logging to LogDispatcher which distributes
 * to registered providers (Console, IndexedDB, Sentry, etc.)
 *
 * Usage:
 * ```typescript
 * private readonly logger = inject(LoggerService).withContext('MyService');
 * this.logger.debug('Something happened', { data });
 * ```
 */
@Injectable({
    providedIn: 'root',
})
export class LoggerService implements Logger {
    private readonly dispatcher = inject(LogDispatcher);
 
    /**
     * Create a context-bound logger instance
     * @param context - Context name to prefix all log messages (e.g., 'AuthService')
     * @returns Logger instance with console-like API
     */
    withContext(context: string): Logger {
        return new ContextLogger(this, context);
    }
 
    /**
     * Log debug message
     */
    debug(message: string, data?: unknown, context?: string): void {
        this.dispatcher.dispatch('debug', message, context, data);
    }
 
    /**
     * Log info message
     */
    info(message: string, data?: unknown, context?: string): void {
        this.dispatcher.dispatch('info', message, context, data);
    }
 
    /**
     * Log warning message
     */
    warn(message: string, data?: unknown, context?: string): void {
        this.dispatcher.dispatch('warn', message, context, data);
    }
 
    /**
     * Log error message
     */
    error(message: string, data?: unknown, context?: string): void {
        this.dispatcher.dispatch('error', message, context, data);
    }
}
 
// Re-export LogLevel for convenience
export { LogLevel };