Skip to content

Commit ad78245

Browse files
committed
fix(logger): support module-scope getLogger()
Problem: Assigning a `const logger = getLogger()` in module scope, may default to `ConsoleLogger`, because modules may be loaded before logging is initialized. Solution: Move the decision into `TopicLogger`, so that the time of the `getLogger()` assignment is irrelevant (just-in-time).
1 parent 545dc4f commit ad78245

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

packages/core/src/shared/fs/watchedFiles.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,15 @@ export abstract class WatchedFiles<T> implements vscode.Disposable {
220220
item: item,
221221
}
222222
} else {
223-
getLogger().info(`${this.name}: failed to process: ${uri}`)
223+
getLogger().debug(`${this.name}: failed to process: ${uri}`)
224224
// if value isn't valid for type, remove from registry
225225
this.registryData.delete(pathAsString)
226226
}
227227
} catch (e) {
228228
if (!quiet) {
229229
throw e
230230
}
231-
getLogger().info(`${this.name}: failed to process: ${uri}: ${(e as Error).message}`)
231+
getLogger().error(`${this.name}: failed to process: ${uri}: ${(e as Error).message}`)
232232
}
233233
return undefined
234234
}

packages/core/src/shared/logger/logger.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import * as vscode from 'vscode'
77

8-
export type LogTopic = 'crashReport' | 'notifications' | 'test' | 'unknown'
8+
export type LogTopic = 'crashReport' | 'dev/beta' | 'notifications' | 'test' | 'unknown'
99

1010
class ErrorLog {
1111
constructor(
@@ -143,24 +143,18 @@ function prependTopic(topic: string, message: string | Error): string | ErrorLog
143143
}
144144

145145
/**
146-
* Gets the logger if it has been initialized
147-
* the logger is of `'main'` or `undefined`: Main logger; default impl: logs to log file and log output channel
146+
* Gets the global default logger.
147+
*
148148
* @param topic: topic to be appended in front of the message.
149149
*/
150150
export function getLogger(topic?: LogTopic): Logger {
151-
const logger = toolkitLoggers['main']
152-
if (!logger) {
153-
return new ConsoleLogger()
154-
}
155-
return new TopicLogger(topic ?? 'unknown', logger)
151+
// `TopicLogger` will lazy-load the "main" logger when it becomes available.
152+
return new TopicLogger(topic ?? 'unknown', 'main')
156153
}
157154

158155
export function getDebugConsoleLogger(topic?: LogTopic): Logger {
159-
const logger = toolkitLoggers['debugConsole']
160-
if (!logger) {
161-
return new ConsoleLogger()
162-
}
163-
return new TopicLogger(topic ?? 'unknown', logger)
156+
// `TopicLogger` will lazy-load the "debugConsole" logger when it becomes available.
157+
return new TopicLogger(topic ?? 'unknown', 'debugConsole')
164158
}
165159

166160
// jscpd:ignore-start
@@ -215,15 +209,25 @@ export class ConsoleLogger extends BaseLogger {
215209
}
216210

217211
/**
218-
* Wraps a `ToolkitLogger` and defers to it for everything except `topic`.
212+
* Wraps the specified `ToolkitLogger` and defers to it for everything except `topic`.
213+
*
214+
* Falls back to `ConsoleLogger` when the logger isn't available yet (during startup).
219215
*/
220216
export class TopicLogger extends BaseLogger implements vscode.Disposable {
217+
// HACK: crude form of "lazy initialization", to support module-scope assignment of
218+
// `getLogger()` without being sensitive to module-load ordering. So even if logging isn't ready
219+
// at the time of the `getLogger` call, it will recover later. (This is a bit hacky, because it
220+
// arguably doesn't belong in `TopicLogger`.)
221+
public get logger() {
222+
return toolkitLoggers[this.loggerKey] ?? new ConsoleLogger()
223+
}
224+
221225
/**
222226
* Wraps a `ToolkitLogger` and defers to it for everything except `topic`.
223227
*/
224228
public constructor(
225229
public override topic: LogTopic,
226-
public readonly logger: Logger
230+
public readonly loggerKey: keyof typeof toolkitLoggers
227231
) {
228232
super()
229233
}

0 commit comments

Comments
 (0)