Skip to content

Commit 9eb37d2

Browse files
authored
Merge #6026 from justinmk3/fixlogger
2 parents feb9155 + a527f96 commit 9eb37d2

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

packages/core/src/dev/beta.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ import { CancellationError } from '../shared/utilities/timeoutUtils'
2121
import { isAmazonQ, isCloud9, productName } from '../shared/extensionUtilities'
2222
import * as config from './config'
2323
import { isReleaseVersion } from '../shared/vscode/env'
24+
import { getRelativeDate } from '../shared/datetime'
2425

2526
const localize = nls.loadMessageBundle()
27+
const logger = getLogger('dev/beta')
2628

2729
const downloadIntervalMs = 1000 * 60 * 60 * 24 // A day in milliseconds
2830

@@ -57,12 +59,13 @@ export async function activate(ctx: vscode.ExtensionContext) {
5759
* If this is the first time we are watching the beta version or if its been 24 hours since it was last checked then try to prompt for update
5860
*/
5961
export function watchBetaVSIX(vsixUrl: string): vscode.Disposable {
60-
getLogger().info(`dev: watching ${vsixUrl} for beta artifacts`)
61-
6262
const toolkit = getBetaToolkitData(vsixUrl)
63+
const lastCheckRel = toolkit ? getRelativeDate(new Date(toolkit.lastCheck)) : ''
64+
logger.info('watching beta artifacts url (lastCheck: %s): %s', lastCheckRel, vsixUrl)
65+
6366
if (!toolkit || toolkit.needUpdate || Date.now() - toolkit.lastCheck > downloadIntervalMs) {
6467
runAutoUpdate(vsixUrl).catch((e) => {
65-
getLogger().error('runAutoUpdate failed: %s', (e as Error).message)
68+
logger.error('runAutoUpdate failed: %s', (e as Error).message)
6669
})
6770
}
6871

@@ -71,13 +74,13 @@ export function watchBetaVSIX(vsixUrl: string): vscode.Disposable {
7174
}
7275

7376
async function runAutoUpdate(vsixUrl: string) {
74-
getLogger().debug(`dev: checking ${vsixUrl} for a new version`)
77+
logger.debug(`checking url for a new version: %s`, vsixUrl)
7578

7679
try {
7780
await telemetry.aws_autoUpdateBeta.run(() => checkBetaUrl(vsixUrl))
7881
} catch (e) {
7982
if (!isUserCancelledError(e)) {
80-
getLogger().warn(`dev: beta extension auto-update failed: %s`, e)
83+
logger.warn('beta extension auto-update failed: %s', e)
8184
}
8285
}
8386
}
@@ -165,7 +168,7 @@ async function promptInstallToolkit(pluginPath: vscode.Uri, newVersion: string,
165168
switch (response) {
166169
case installBtn:
167170
try {
168-
getLogger().info(`dev: installing artifact ${vsixName}`)
171+
logger.info(`installing artifact: ${vsixName}`)
169172
await vscode.commands.executeCommand('workbench.extensions.installExtension', pluginPath)
170173
await updateBetaToolkitData(vsixUrl, {
171174
lastCheck: Date.now(),

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)