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 | 7x 32x 32x 32x 30x 18x 1x 17x 3x 14x 14x 14x 14x 14x 14x 3x 11x 14x 1x 10x 1x 2x | import { LogEntry, LogProvider } from '../log-provider.interface';
/**
* Console log provider
* Outputs logs to browser console with formatting
*
* In production mode: only 'error' level is output
* In development mode: all levels are output
*/
export class ConsoleLogProvider implements LogProvider {
readonly name = 'console';
constructor(
private readonly isProduction: boolean,
private readonly isBrowser: boolean
) {}
get isAvailable(): boolean {
return this.isBrowser;
}
log(entry: LogEntry): void {
if (!this.isAvailable) {
return;
}
// In production, only output errors to console
if (this.isProduction && entry.level !== 'error') {
return;
}
this.outputToConsole(entry);
}
private outputToConsole(entry: LogEntry): void {
const prefix = entry.context ? `[${entry.context}]` : '';
const timestamp = entry.timestamp.toISOString();
const formattedMessage = `${timestamp} ${prefix} ${entry.message}`;
const consoleMethod = this.getConsoleMethod(entry.level);
if (entry.data !== undefined) {
consoleMethod(formattedMessage, entry.data);
} else {
consoleMethod(formattedMessage);
}
}
private getConsoleMethod(level: LogEntry['level']): (...args: unknown[]) => void {
switch (level) {
case 'debug':
return console.debug.bind(console);
case 'info':
return console.info.bind(console);
case 'warn':
return console.warn.bind(console);
case 'error':
return console.error.bind(console);
}
}
}
|