diff --git a/src/logged.ts b/src/logged.ts index d08c2ee..3983517 100644 --- a/src/logged.ts +++ b/src/logged.ts @@ -138,18 +138,9 @@ function overrideBuild, R>( args.length <= metadatas.scopedLoggerInjectableParam || !(args[metadatas.scopedLoggerInjectableParam] instanceof ScopedLogger) ) { - args[metadatas.scopedLoggerInjectableParam] = new ScopedLogger( - baseLogger, - key, - true, - true, - ); + args[metadatas.scopedLoggerInjectableParam] = ScopedLogger.fromRoot(baseLogger, key); } else { - args[metadatas.scopedLoggerInjectableParam] = new ScopedLogger( - args[metadatas.scopedLoggerInjectableParam], - key, - false - ); + args[metadatas.scopedLoggerInjectableParam] = ScopedLogger.fromSuper(baseLogger, args[metadatas.scopedLoggerInjectableParam], key); } injectedLogger = args[metadatas.scopedLoggerInjectableParam]; diff --git a/src/logger.ts b/src/logger.ts index c6a4f60..f0e9b4f 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -6,24 +6,19 @@ const createId = hyperid({ fixedLength: true }) type LogLevel = "debug" | "log" | "warn" | "verbose" | "error" | "fatal"; export class ScopedLogger extends Logger { - private readonly scopeId?: string; - constructor( private logger: Logger, - private scope: string, - private root: boolean = false, - private createScopeId: boolean = false, + private scope: string[], + private scopeId: string = createId(), ) { super(); - if (this.createScopeId) this.scopeId = createId(); } private scopedLog(method: LogLevel) { return (message: string) => { this.logger[method]( - `${this.root ? "" : "-> "}${this.scope}${ - this.scopeId ? `(${this.scopeId})` : "" - }: ${message}` + `${this.scopeId ? `(ID ${this.scopeId}) | ` : "" + }${this.scope.join(" -> ")}: ${message}` ); }; } @@ -34,4 +29,15 @@ export class ScopedLogger extends Logger { verbose = this.scopedLog("verbose"); error = this.scopedLog("error"); fatal = this.scopedLog("fatal"); -} + + static fromSuper(baseLogger: Logger, logger: ScopedLogger, scope: string): ScopedLogger { + return new ScopedLogger( + baseLogger, [...logger.scope, scope], logger.scopeId + ) + }; + static fromRoot(logger: Logger, scope: string): ScopedLogger { + return new ScopedLogger( + logger, [scope] + ) + }; +} \ No newline at end of file