fix(manager): correct getLogger and implement hierarchy setup

Fix inverted type check in getLogger() and add missing return
statement. Implement _fixupParents() and _fixupChildren() to
establish parent-child logger relationships based on dot-separated
scope names.
This commit is contained in:
Tiara Rodney 2026-03-13 22:39:35 +01:00
parent df0126693e
commit 03e3641e03
No known key found for this signature in database
GPG key ID: 5CD8EC1D46106723

View file

@ -71,20 +71,73 @@ export class Manager {
* up the parent/child references which pointed to the placeholder to now
* point to the logger.
*/
getLogger(scope: string) {
var rv: null|Logger = null;
getLogger(scope: string): Logger {
let rv: Logger;
if (typeof scope != 'string') {
if (scope in this.loggers) {
const existing = this.loggers[scope];
rv = this.loggers[scope];
if (rv instanceof Placeholder) {
var ph = rv;
if (existing instanceof Placeholder) {
rv = new (this._loggerClass ?? loggerClass)(scope, NOTSET);
this.loggers[scope] = rv;
this._fixupChildren(existing, rv);
}
else {
rv = existing;
}
}
else {
rv = new (this._loggerClass ?? loggerClass)(scope, NOTSET);
this.loggers[scope] = rv;
this._fixupParents(rv);
}
return rv;
}
/**
* Ensure that there are either loggers or placeholders all the way from
* the specified logger to the root of the logger hierarchy.
*/
protected _fixupParents(logger: Logger) {
const name = logger.scope;
let i = name.lastIndexOf('.');
let rv: Logger | null = null;
while (i > 0 && !rv) {
const substr = name.substring(0, i);
if (!(substr in this.loggers)) {
this.loggers[substr] = new Placeholder(logger) as unknown as Logger;
}
else {
const obj = this.loggers[substr];
if (obj instanceof Placeholder) {
obj.push(logger);
}
else {
rv = obj;
}
}
i = name.lastIndexOf('.', i - 1);
}
(logger as unknown as { parent: Logger }).parent = rv ?? this.root;
}
/**
* Ensure that children of the placeholder ph are connected to the
* specified logger.
*/
protected _fixupChildren(ph: Placeholder, logger: Logger) {
const name = logger.scope;
for (const c of ph.loggers) {
if (c.parent!.scope.substring(0, name.length) !== name) {
(logger as unknown as { parent: Logger }).parent = c.parent!;
(c as unknown as { parent: Logger }).parent = logger;
}
}
}