| 10 | const logTransports = [new transports.Console()]; |
| 11 | |
| 12 | export class Logger implements ILogger { |
| 13 | private logger: WindstonLogger; |
| 14 | public level: LogLevel; |
| 15 | |
| 16 | constructor(private readonly options: LoggerOptions) { |
| 17 | this.level = options.logLevel ?? LogLevel.Debug; |
| 18 | |
| 19 | const format = this.getLoggerFormat(); |
| 20 | this.logger = createLogger({ |
| 21 | defaultMeta: { |
| 22 | component: options.component, |
| 23 | ...options.metadata, |
| 24 | }, |
| 25 | level: this.level, |
| 26 | format, |
| 27 | transports: logTransports, |
| 28 | handleExceptions: true, |
| 29 | exitOnError: true, |
| 30 | exceptionHandlers: logTransports, |
| 31 | rejectionHandlers: logTransports, |
| 32 | }); |
| 33 | } |
| 34 | |
| 35 | public getLoggerFormat(): Format { |
| 36 | const developmentFormats: Format[] = [ |
| 37 | format.timestamp(), |
| 38 | format.errors({ stack: true, cause: true }), |
| 39 | customFormat(), |
| 40 | ]; |
| 41 | if (this.options.additionalDevelopmentFormats) { |
| 42 | developmentFormats.push(...this.options.additionalDevelopmentFormats); |
| 43 | } |
| 44 | if (this.options.additionalFormats) { |
| 45 | developmentFormats.push(...this.options.additionalFormats); |
| 46 | } |
| 47 | |
| 48 | const productionFormats: Format[] = [ |
| 49 | format.timestamp(), |
| 50 | format.errors({ stack: true, cause: true }), |
| 51 | format.json(), |
| 52 | ]; |
| 53 | if (this.options.additionalFormats) { |
| 54 | productionFormats.push(...this.options.additionalFormats); |
| 55 | } |
| 56 | |
| 57 | return this.options.isProduction |
| 58 | ? format.combine(...productionFormats) |
| 59 | : format.combine(...developmentFormats); |
| 60 | } |
| 61 | |
| 62 | debug(message: string, params?: Record<string, unknown>): void { |
| 63 | this.logger.debug(message, params); |
| 64 | } |
| 65 | |
| 66 | info(message: string, params?: Record<string, unknown>): void { |
| 67 | this.logger.info(message, params); |
| 68 | } |
| 69 |
nothing calls this directly
no outgoing calls
no test coverage detected