|
1 | | -import { LogInstanceNameFn } from './decorators/log'; |
2 | 1 | import type { LogLevel } from './logger.constants'; |
3 | 2 | import type { LogScope } from './logger.scope'; |
4 | 3 | import { padOrTruncateEnd } from './string'; |
@@ -317,28 +316,26 @@ function toOrderedLevel(logLevel: LogLevel): OrderedLevel { |
317 | 316 | } |
318 | 317 | } |
319 | 318 |
|
320 | | -// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type |
321 | | -export function getLoggableName(instance: Function | object): string { |
| 319 | +export const customLoggableNameFns = new WeakMap<object, (instance: any, name: string) => string>(); |
| 320 | + |
| 321 | +export function getLoggableName(instance: object): string { |
322 | 322 | let ctor; |
323 | 323 | if (typeof instance === 'function') { |
324 | | - if (instance.prototype?.constructor == null) return instance.name; |
325 | | - |
326 | | - ctor = instance.prototype.constructor; |
| 324 | + ctor = instance.prototype?.constructor; |
| 325 | + if (ctor == null) return instance.name; |
327 | 326 | } else { |
328 | 327 | ctor = instance.constructor; |
329 | 328 | } |
330 | 329 |
|
331 | 330 | let name: string = ctor?.name ?? ''; |
332 | | - |
333 | 331 | // Strip webpack module name (since I never name classes with an _) |
334 | 332 | const index = name.indexOf('_'); |
335 | | - name = index === -1 ? name : name.substring(index + 1); |
336 | | - |
337 | | - if (ctor?.[LogInstanceNameFn] != null) { |
338 | | - name = ctor[LogInstanceNameFn](instance, name); |
| 333 | + if (index !== -1) { |
| 334 | + name = name.substring(index + 1); |
339 | 335 | } |
340 | 336 |
|
341 | | - return name; |
| 337 | + const customNameFn = customLoggableNameFns.get(ctor); |
| 338 | + return customNameFn?.(instance, name) ?? name; |
342 | 339 | } |
343 | 340 |
|
344 | 341 | export interface LogProvider { |
|
0 commit comments