Skip to content

Commit 87c5853

Browse files
committed
feat(dev-mode): always log to aws.dev.logfile at "debug" log-level
Problem: When debugging the extension, vscode tends to ignore the log-level that was set from the `AWS Toolkit Logs` UI. Solution: If the dev-mode `aws.dev.logfile` path is configured, log to it at "debug" log-level regardless of the (initial) `vscode.LogOutputChannel.logLevel`.
1 parent 16aa368 commit 87c5853

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

CONTRIBUTING.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,16 @@ The `aws.dev.forceDevMode` setting enables or disables Toolkit "dev mode". Witho
345345
- Example: `getLogger().error('topic: widget failed: %O', { foo: 'bar', baz: 42 })`
346346
- Log messages are written to the extension Output channel, which you can view in vscode by visiting the "Output" panel and selecting `AWS Toolkit Logs` or `Amazon Q Logs`.
347347
- Use the `aws.dev.logfile` setting to set the logfile path to a fixed location, so you can follow
348-
and filter logs using shell tools like `tail` and `grep`. For example in settings.json,
349-
```
350-
"aws.dev.logfile": "~/awstoolkit.log",
351-
```
352-
then you can tail the logfile in your terminal:
353-
```
354-
tail -F ~/awstoolkit.log
355-
```
348+
and filter logs using shell tools like `tail` and `grep`.
349+
- Note: this always logs at **debug log-level** (though you can temporarily override that from the `AWS Toolkit Logs` UI).
350+
- Example `settings.json`:
351+
```
352+
"aws.dev.logfile": "~/awstoolkit.log",
353+
```
354+
then you can tail the logfile in your terminal:
355+
```
356+
tail -F ~/awstoolkit.log
357+
```
356358
- Use the `AWS (Developer): Watch Logs` command to watch and filter Toolkit logs (including
357359
telemetry) in VSCode.
358360
- Only available if you enabled "dev mode" (`aws.dev.forceDevMode` setting, see above).

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ export async function activate(
3535

3636
const mainLogger = makeLogger({
3737
logLevel: chanLogLevel,
38-
logPaths: logUri ? [logUri] : undefined,
38+
logFile: logUri,
3939
outputChannels: [logChannel],
4040
useConsoleLog: isWeb(),
4141
})
4242
logChannel.onDidChangeLogLevel?.((logLevel) => {
4343
const newLogLevel = fromVscodeLogLevel(logLevel)
4444
mainLogger.setLogLevel(newLogLevel) // Also logs a message.
4545
})
46+
mainLogger.setLogLevel('debug') // HACK: set to "debug" when debugging the extension.
4647

4748
setLogger(mainLogger)
4849

@@ -56,7 +57,7 @@ export async function activate(
5657
'debugConsole'
5758
)
5859

59-
getLogger().info('Log level: %s%s', chanLogLevel, logUri ? `, file: ${logUri.fsPath}` : '')
60+
getLogger().info('Log level: %s%s', chanLogLevel, logUri ? `, file (always "debug" level): ${logUri.fsPath}` : '')
6061
getLogger().debug('User agent: %s', getUserAgent({ includePlatform: true, includeClientId: true }))
6162
if (devLogfile && typeof devLogfile !== 'string') {
6263
getLogger().error('invalid aws.dev.logfile setting')
@@ -69,20 +70,23 @@ export async function activate(
6970
/**
7071
* Creates a logger off of specified params
7172
* @param opts.logLevel Log messages at or above this level
72-
* @param opts.logPaths Array of paths to output log entries to
73+
* @param opts.logFile See {@link Logger.logFile}
7374
* @param opts.outputChannels Array of output channels to log entries to
7475
* @param opts.useConsoleLog If true, outputs log entries to the nodejs or browser devtools console.
7576
*/
7677
export function makeLogger(opts: {
7778
logLevel: LogLevel
78-
logPaths?: vscode.Uri[]
79+
logFile?: vscode.Uri
7980
outputChannels?: vscode.OutputChannel[]
8081
useConsoleLog?: boolean
8182
}): Logger {
8283
const logger = new ToolkitLogger(opts.logLevel)
83-
// debug console can show ANSI colors, output channels can not
84-
for (const logPath of opts.logPaths ?? []) {
85-
logger.logToFile(logPath)
84+
if (opts.logFile) {
85+
logger.logToFile(opts.logFile)
86+
logger.logFile = opts.logFile
87+
// XXX: `vscode.LogOutputChannel` does not support programmatically setting the log-level,
88+
// so this has no effect there. But this at least enables sinking to `SharedFileTransport`.
89+
logger.setLogLevel('debug')
8690
}
8791
for (const outputChannel of opts.outputChannels ?? []) {
8892
logger.logToOutputChannel(outputChannel)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ const toolkitLoggers: {
1212
} = { main: undefined, debugConsole: undefined }
1313

1414
export interface Logger {
15+
/**
16+
* Developer-only: Optional log file, which gets all log messages (regardless of the configured
17+
* log-level).
18+
*/
19+
logFile?: vscode.Uri
1520
debug(message: string | Error, ...meta: any[]): number
1621
verbose(message: string | Error, ...meta: any[]): number
1722
info(message: string | Error, ...meta: any[]): number
@@ -27,6 +32,8 @@ export interface Logger {
2732
}
2833

2934
export abstract class BaseLogger implements Logger {
35+
logFile?: vscode.Uri
36+
3037
debug(message: string | Error, ...meta: any[]): number {
3138
return this.sendToLog('debug', message, ...meta)
3239
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('makeLogger', function () {
1818
before(async function () {
1919
tempFolder = await makeTemporaryToolkitFolder()
2020
const logPath = vscode.Uri.joinPath(vscode.Uri.file(tempFolder), 'log.txt')
21-
testLogger = makeLogger({ logLevel: 'debug', logPaths: [logPath] })
21+
testLogger = makeLogger({ logLevel: 'debug', logFile: logPath })
2222
})
2323

2424
after(async function () {

0 commit comments

Comments
 (0)