| 86 | */ |
| 87 | @Injectable() |
| 88 | export class Logger implements LoggerService { |
| 89 | protected static logBuffer = new Array<LogBufferRecord>(); |
| 90 | protected static staticInstanceRef?: LoggerService = DEFAULT_LOGGER; |
| 91 | protected static logLevels?: LogLevel[]; |
| 92 | private static isBufferAttached: boolean; |
| 93 | |
| 94 | protected localInstanceRef?: LoggerService; |
| 95 | |
| 96 | private static WrapBuffer: MethodDecorator = ( |
| 97 | target: object, |
| 98 | propertyKey: string | symbol, |
| 99 | descriptor: TypedPropertyDescriptor<any>, |
| 100 | ) => { |
| 101 | const originalFn = descriptor.value; |
| 102 | descriptor.value = function (...args: unknown[]) { |
| 103 | if (Logger.isBufferAttached) { |
| 104 | Logger.logBuffer.push({ |
| 105 | methodRef: originalFn.bind(this), |
| 106 | arguments: args, |
| 107 | }); |
| 108 | return; |
| 109 | } |
| 110 | return originalFn.call(this, ...args); |
| 111 | }; |
| 112 | }; |
| 113 | |
| 114 | constructor(); |
| 115 | constructor(context: string); |
| 116 | constructor(context: string, options?: { timestamp?: boolean }); |
| 117 | constructor( |
| 118 | @Optional() protected context?: string, |
| 119 | @Optional() protected options: { timestamp?: boolean } = {}, |
| 120 | ) {} |
| 121 | |
| 122 | get localInstance(): LoggerService { |
| 123 | if (Logger.staticInstanceRef === DEFAULT_LOGGER) { |
| 124 | return this.registerLocalInstanceRef(); |
| 125 | } else if (Logger.staticInstanceRef instanceof Logger) { |
| 126 | const prototype = Object.getPrototypeOf(Logger.staticInstanceRef); |
| 127 | if (prototype.constructor === Logger) { |
| 128 | return this.registerLocalInstanceRef(); |
| 129 | } |
| 130 | } |
| 131 | return Logger.staticInstanceRef!; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Write an 'error' level log. |
| 136 | */ |
| 137 | error(message: any, stack?: string, context?: string): void; |
| 138 | error(message: any, ...optionalParams: [...any, string?, string?]): void; |
| 139 | @Logger.WrapBuffer |
| 140 | error(message: any, ...optionalParams: any[]) { |
| 141 | optionalParams = this.context |
| 142 | ? (optionalParams.length ? optionalParams : [undefined]).concat( |
| 143 | this.context, |
| 144 | ) |
| 145 | : optionalParams; |