Skip to content

Commit

Permalink
Fix bug causing multiple instances of LogTape to import separate Logg…
Browse files Browse the repository at this point in the history
…er instances
  • Loading branch information
dahlia committed May 7, 2024
1 parent c2d39f2 commit a91c4a4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Version 0.1.2

To be released.

- Fixed a bug where two or more versions of LogTape were imported in the same
runtime, the `Logger` instances would not be shared between them. This was
caused by the `Logger` instances being stored in a module-level variable.


Version 0.1.1
-------------
Expand Down
16 changes: 14 additions & 2 deletions logtape/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,16 @@ export function getLogger(category: string | readonly string[] = []): Logger {
}

/**
* The root logger.
* The symbol for the global root logger.
*/
let rootLogger: LoggerImpl | null = null;
const globalRootLoggerSymbol = Symbol.for("logtape.rootLogger");

/**
* The global root logger registry.
*/
interface GlobalRootLoggerRegistry {
[globalRootLoggerSymbol]?: LoggerImpl;
}

/**
* A logger implementation. Do not use this directly; use {@link getLogger}
Expand All @@ -372,8 +379,13 @@ export class LoggerImpl implements Logger {
readonly filters: Filter[];

static getLogger(category: string | readonly string[] = []): LoggerImpl {
let rootLogger: LoggerImpl | null = globalRootLoggerSymbol in globalThis
? (globalThis as GlobalRootLoggerRegistry)[globalRootLoggerSymbol] ?? null
: null;
if (rootLogger == null) {
rootLogger = new LoggerImpl(null, []);
(globalThis as GlobalRootLoggerRegistry)[globalRootLoggerSymbol] =
rootLogger;
}
if (typeof category === "string") return rootLogger.getChild(category);
if (category.length === 0) return rootLogger;
Expand Down

0 comments on commit a91c4a4

Please sign in to comment.