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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | 1x 1842x 1842x 1842x 1842x 1842x 1842x 921x 921x 921x 921x 921x 952x 24x 24x 24x 24x 1033x 700x 700x 700x 700x 700x 700x 700x 1400x 1374x 26x 26x 1033x 1033x 700x 1033x | import { Inject, Injectable } from '@angular/core'; import { LogPublisher } from './publishers/publisher/log-publisher'; import { LogPublishersService } from './publishers/log-publishers.service'; import { LogEntry } from './logEntry/logEntry'; import { LogLevel } from './logLevel/logLevel'; import { IEnvironment } from 'src/environments/interface/ienvironment'; import { ENV } from 'src/environments/injectionToken/environment-provider'; /** * Service responsible for logging using every {@link LogPublisher} given by the * {@link LogPublishersService}. Inspired by * https://www.codemag.com/article/1711021/Logging-in-Angular-Applications */ @Injectable({ providedIn: 'root', }) export class LogService { /** Log level : minimal log level actually logged. */ level: LogLevel = this.environment.logLevel; /** Does the log contains the date and time of the log. */ logWithDate = this.environment.logWithDate; /** {@link LogPublisher} used to log the {@link LogEntry} */ publishers: LogPublisher[]; /** * Class name used in the log, used to identify where the log comes from in * the code, and should be specified when creating a logger. */ className = 'none specified'; /** * Log service constructor. * * @param environment The environment * @param publishersService The {@link LogPublishersService} */ constructor( @Inject(ENV) private environment: IEnvironment, private publishersService: LogPublishersService ) { // Set publishers this.publishers = this.publishersService.publishers; } /** * Used to create a new logger with a specific class name * * @param name The name of the classe * @returns The logger */ withClassName(name: string) { const res = new LogService(this.environment, this.publishersService); res.level = this.level; res.logWithDate = this.logWithDate; res.className = name; return res; } /** * Logs a debug message with optional objects * * @param msg The message * @param optionalParams The optional objects */ debug(msg: string, ...optionalParams: unknown[]) { this.writeToLog(msg, LogLevel.Debug, optionalParams); } /** * Logs a info message with optional objects * * @param msg The message * @param optionalParams The optional objects */ info(msg: string, ...optionalParams: unknown[]) { this.writeToLog(msg, LogLevel.Info, optionalParams); } /** * Logs a warn message with optional objects * * @param msg The message * @param optionalParams The optional objects */ warn(msg: string, ...optionalParams: unknown[]) { this.writeToLog(msg, LogLevel.Warn, optionalParams); } /** * Logs a error message with optional objects * * @param msg The message * @param optionalParams The optional objects */ error(msg: string, ...optionalParams: unknown[]) { this.writeToLog(msg, LogLevel.Error, optionalParams); } /** * Logs a fatal message with optional objects * * @param msg The message * @param optionalParams The optional objects */ fatal(msg: string, ...optionalParams: unknown[]) { this.writeToLog(msg, LogLevel.Fatal, optionalParams); } /** * Creates a {@link LogEntry} with a messages and optional objects and logs it * at a given {@link LogLevel} using the {@link LogPublisher}. * * @param msg The message * @param level The {@link LogLevel} * @param params The optional objects */ private writeToLog(msg: string, level: LogLevel, params: unknown[]) { if (this.shouldLog(level)) { const entry: LogEntry = new LogEntry(this.environment); entry.message = msg; entry.level = level; entry.extraInfo = params; entry.logWithDate = this.logWithDate; entry.className = this.className; for (const logger of this.publishers) { logger.log(entry).subscribe({ next: (response) => { if (!response) { // eslint-disable-next-line no-console console.log( 'Erreur avec loggeur ' + logger.constructor.name + ' ' + logger.location ); } }, error: () => { // eslint-disable-next-line no-console console.log( 'Erreur avec loggeur ' + logger.constructor.name + ' ' + logger.location ); }, complete: undefined, }); } } } /** * Tests whether or not a {@link LogEntry} should be logged at the given level. * * @param level The log level to test * @returns Whether or not the {@link LogEntry} should be logged */ private shouldLog(level: LogLevel): boolean { let ret = false; if ( (level >= this.level && this.level !== LogLevel.Off) || this.level === LogLevel.All ) { ret = true; } return ret; } } |