Skip to content

Commit 5eea5f9

Browse files
authored
Merge pull request #5564 from tomcat323/betterLoggingWithHeaders
refactor(logging): removed types input in getLogger()
2 parents e291f42 + 8bfe1ff commit 5eea5f9

17 files changed

+62
-93
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ You can also use these NPM tasks (see `npm run` for the full list):
133133
1. Declare a global unhandledRejection handler.
134134
```ts
135135
process.on('unhandledRejection', (e) => {
136-
getLogger('channel').error(
136+
getLogger().error(
137137
localize(
138138
'AWS.channel.aws.toolkit.activation.error',
139139
'Error Activating {0} Toolkit: {1}',

packages/core/src/extensionNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export async function activate(context: vscode.ExtensionContext) {
237237
if (stacktrace !== undefined && stacktrace.length > 40) {
238238
stacktrace.length = 40
239239
}
240-
getLogger('channel').error(
240+
getLogger().error(
241241
localize(
242242
'AWS.channel.aws.toolkit.activation.error',
243243
'Error Activating {0} Toolkit: {1} \n{2}',

packages/core/src/lambda/commands/createNewSamApp.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export async function resumeCreateNewSamApp(
100100
reason = 'error'
101101

102102
globals.outputChannel.show(true)
103-
getLogger('channel').error(
103+
getLogger().error(
104104
localize('AWS.samcli.initWizard.resume.error', 'Error resuming SAM Application creation. {0}', checklogs())
105105
)
106106

@@ -235,7 +235,7 @@ export async function createNewSamApplication(
235235
destinationDirectory: vscode.Uri.file(destinationDirectory),
236236
}
237237
schemaCodeDownloader = createSchemaCodeDownloaderObject(client!, globals.outputChannel)
238-
getLogger('channel').info(
238+
getLogger().info(
239239
localize(
240240
'AWS.message.info.schemas.downloadCodeBindings.start',
241241
'Downloading code for schema {0}...',
@@ -333,7 +333,7 @@ export async function createNewSamApplication(
333333
reason = getTelemetryReason(err)
334334

335335
globals.outputChannel.show(true)
336-
getLogger('channel').error(
336+
getLogger().error(
337337
localize('AWS.samcli.initWizard.general.error', 'Error creating new SAM Application. {0}', checklogs())
338338
)
339339

packages/core/src/lambda/commands/deploySamApplication.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ async function buildOperation(params: {
158158
invoker: SamCliProcessInvoker
159159
}): Promise<boolean> {
160160
try {
161-
getLogger('channel').info(localize('AWS.samcli.deploy.workflow.init', 'Building SAM Application...'))
161+
getLogger().info(localize('AWS.samcli.deploy.workflow.init', 'Building SAM Application...'))
162162

163163
const buildDestination = getBuildRootFolder(params.deployParameters.deployRootFolder)
164164

@@ -173,7 +173,7 @@ async function buildOperation(params: {
173173

174174
return true
175175
} catch (err) {
176-
getLogger('channel').warn(
176+
getLogger().warn(
177177
localize(
178178
'AWS.samcli.build.failedBuild',
179179
'"sam build" failed: {0}',
@@ -200,7 +200,7 @@ async function packageOperation(
200200
)
201201
}
202202

203-
getLogger('channel').info(
203+
getLogger().info(
204204
localize(
205205
'AWS.samcli.deploy.workflow.packaging',
206206
'Packaging SAM Application to S3 Bucket: {0}',
@@ -232,7 +232,7 @@ async function deployOperation(params: {
232232
invoker: SamCliProcessInvoker
233233
}): Promise<void> {
234234
try {
235-
getLogger('channel').info(
235+
getLogger().info(
236236
localize(
237237
'AWS.samcli.deploy.workflow.stackName.initiated',
238238
'Deploying SAM Application to CloudFormation Stack: {0}',
@@ -273,13 +273,13 @@ async function deploy(params: {
273273
window: WindowFunctions
274274
}): Promise<void> {
275275
globals.outputChannel.show(true)
276-
getLogger('channel').info(localize('AWS.samcli.deploy.workflow.start', 'Starting SAM Application deployment...'))
276+
getLogger().info(localize('AWS.samcli.deploy.workflow.start', 'Starting SAM Application deployment...'))
277277

278278
const buildSuccessful = await buildOperation(params)
279279
await packageOperation(params, buildSuccessful)
280280
await deployOperation(params)
281281

282-
getLogger('channel').info(
282+
getLogger().info(
283283
localize(
284284
'AWS.samcli.deploy.workflow.success',
285285
'Deployed SAM Application to CloudFormation Stack: {0}',
@@ -311,10 +311,10 @@ function enhanceAwsCloudFormationInstructions(
311311
}
312312

313313
function outputDeployError(error: Error) {
314-
getLogger('channel').error(error)
314+
getLogger().error(error)
315315

316316
globals.outputChannel.show(true)
317-
getLogger('channel').error('AWS.samcli.deploy.general.error', 'Error deploying a SAM Application. {0}', checklogs())
317+
getLogger().error('AWS.samcli.deploy.general.error', 'Error deploying a SAM Application. {0}', checklogs())
318318
}
319319

320320
function getDefaultWindowFunctions(): WindowFunctions {

packages/core/src/lambda/local/debugConfiguration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export function getArchitecture(
256256
const isArch = isArchitecture(arch)
257257

258258
if (!isArch) {
259-
getLogger('channel').warn('SAM Invoke: Invalid architecture. Defaulting to x86_64.')
259+
getLogger().warn('SAM Invoke: Invalid architecture. Defaulting to x86_64.')
260260
void vscode.window.showWarningMessage(
261261
localize(
262262
'AWS.output.sam.invalidArchitecture',

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

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,12 @@ export async function activate(
4646

4747
setLogger(mainLogger)
4848

49-
// Logs to "AWS Toolkit" output channel.
50-
setLogger(
51-
makeLogger({
52-
logLevel: chanLogLevel,
53-
logPaths: logUri ? [logUri] : undefined,
54-
outputChannels: [outputChannel, logChannel],
55-
}),
56-
'channel'
57-
)
58-
5949
// Logs to vscode Debug Console.
6050
setLogger(
6151
makeLogger({
6252
logLevel: chanLogLevel,
6353
outputChannels: [outputChannel, logChannel],
64-
useDebugConsole: true,
54+
useConsoleLog: true,
6555
}),
6656
'debugConsole'
6757
)
@@ -81,27 +71,21 @@ export async function activate(
8171
* @param opts.logLevel Log messages at or above this level
8272
* @param opts.logPaths Array of paths to output log entries to
8373
* @param opts.outputChannels Array of output channels to log entries to
84-
* @param opts.useDebugConsole If true, outputs log entries to `vscode.debug.activeDebugConsole`
8574
* @param opts.useConsoleLog If true, outputs log entries to the nodejs or browser devtools console.
8675
*/
8776
export function makeLogger(opts: {
8877
logLevel: LogLevel
8978
logPaths?: vscode.Uri[]
9079
outputChannels?: vscode.OutputChannel[]
91-
useDebugConsole?: boolean
9280
useConsoleLog?: boolean
9381
}): Logger {
9482
const logger = new WinstonToolkitLogger(opts.logLevel)
9583
// debug console can show ANSI colors, output channels can not
96-
const stripAnsi = opts.useDebugConsole ?? false
9784
for (const logPath of opts.logPaths ?? []) {
9885
logger.logToFile(logPath)
9986
}
10087
for (const outputChannel of opts.outputChannels ?? []) {
101-
logger.logToOutputChannel(outputChannel, stripAnsi)
102-
}
103-
if (opts.useDebugConsole) {
104-
logger.logToDebugConsole()
88+
logger.logToOutputChannel(outputChannel)
10589
}
10690
if (opts.useConsoleLog) {
10791
logger.logToConsole()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ export type Loggable = Error | string
1212
export type Logger = logger.Logger
1313
export type LogLevel = logger.LogLevel
1414
export const getLogger = logger.getLogger
15+
export const getDebugConsoleLogger = logger.getDebugConsoleLogger
1516
export const getNullLogger = logger.getNullLogger

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,21 @@ export function compareLogLevel(l1: LogLevel, l2: LogLevel): number {
8484

8585
/**
8686
* Gets the logger if it has been initialized
87-
* @param type Gets the logger type:
88-
* * `'main'` or `undefined`: Main logger; default impl: logs to log file and log output channel
89-
* * `'channel'`: Channel Logger; default impl: logs to the `main` channels and the `AWS Toolkit` output channel
90-
* * `'debug'`: Debug Console Logger; default impl: logs to the `channel` channels and the currently-active VS Code Debug Console pane.
87+
* the logger is of `'main'` or `undefined`: Main logger; default impl: logs to log file and log output channel
9188
*/
92-
export function getLogger(type?: keyof typeof toolkitLoggers): Logger {
93-
const logger = toolkitLoggers[type ?? 'main']
89+
export function getLogger(): Logger {
90+
const logger = toolkitLoggers['main']
9491
if (!logger) {
9592
return new ConsoleLogger()
9693
}
9794

9895
return logger
9996
}
10097

98+
export function getDebugConsoleLogger(): Logger {
99+
return toolkitLoggers['debugConsole'] ?? new ConsoleLogger()
100+
}
101+
101102
export class NullLogger implements Logger {
102103
public setLogLevel(logLevel: LogLevel) {}
103104
public logLevelEnabled(logLevel: LogLevel): boolean {
@@ -187,14 +188,14 @@ export class ConsoleLogger implements Logger {
187188
public enableDebugConsole(): void {}
188189
}
189190

190-
export function getNullLogger(type?: 'channel' | 'debugConsole' | 'main'): Logger {
191+
export function getNullLogger(type?: 'debugConsole' | 'main'): Logger {
191192
return new NullLogger()
192193
}
193194
/**
194195
* Sets (or clears) the logger that is accessible to code.
195196
* The Extension is expected to call this only once per log type.
196197
* Tests should call this to set up a logger prior to executing code that accesses a logger.
197198
*/
198-
export function setLogger(logger: Logger | undefined, type?: 'channel' | 'debugConsole' | 'main') {
199+
export function setLogger(logger: Logger | undefined, type?: 'debugConsole' | 'main') {
199200
toolkitLoggers[type ?? 'main'] = logger
200201
}

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class WinstonToolkitLogger implements Logger, vscode.Disposable {
4444
}
4545

4646
public enableDebugConsole(): void {
47-
this.logToDebugConsole()
47+
this.logToConsole()
4848
}
4949

5050
public setLogLevel(logLevel: LogLevel) {
@@ -74,7 +74,7 @@ export class WinstonToolkitLogger implements Logger, vscode.Disposable {
7474
this.logger.add(fileTransport)
7575
}
7676

77-
public logToOutputChannel(outputChannel: vscode.OutputChannel, stripAnsi: boolean): void {
77+
public logToOutputChannel(outputChannel: vscode.OutputChannel, stripAnsi: boolean = false): void {
7878
const outputChannelTransport: winston.transport = new OutputChannelTransport({
7979
outputChannel,
8080
stripAnsi,
@@ -84,16 +84,6 @@ export class WinstonToolkitLogger implements Logger, vscode.Disposable {
8484
this.logger.add(outputChannelTransport)
8585
}
8686

87-
public logToDebugConsole(): void {
88-
const debugConsoleTransport = new OutputChannelTransport({
89-
name: 'DebugConsole',
90-
outputChannel: vscode.debug.activeDebugConsole,
91-
})
92-
const debugConsoleUri: vscode.Uri = vscode.Uri.parse('console://debug')
93-
debugConsoleTransport.on('logged', (obj: any) => this.parseLogObject(debugConsoleUri, obj))
94-
this.logger.add(debugConsoleTransport)
95-
}
96-
9787
public logToConsole(): void {
9888
const consoleLogTransport: winston.transport = new ConsoleLogTransport({})
9989
const logConsoleUri: vscode.Uri = vscode.Uri.parse('console://log')

packages/core/src/shared/sam/cli/samCliInvoker.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class DefaultSamCliProcessInvoker implements SamCliProcessInvoker {
3636
const invokeOptions = makeRequiredSamCliProcessInvokeOptions(options)
3737
const logging = options?.logging !== false
3838
const getLogger = logging ? logger.getLogger : logger.getNullLogger
39+
const getDebugConsoleLogger = logging ? logger.getDebugConsoleLogger : logger.getNullLogger
3940
const log = getLogger()
4041

4142
const sam = await this.context.getOrDetectSamCli()
@@ -51,16 +52,16 @@ export class DefaultSamCliProcessInvoker implements SamCliProcessInvoker {
5152
spawnOptions: await addTelemetryEnvVar(options?.spawnOptions),
5253
})
5354

54-
getLogger('channel').info(localize('AWS.running.command', 'Command: {0}', `${this.childProcess}`))
55+
getLogger().info(localize('AWS.running.command', 'Command: {0}', `${this.childProcess}`))
5556
log.verbose(`running: ${this.childProcess}`)
5657
return await this.childProcess.run({
5758
onStdout: (text, context) => {
58-
getLogger('debugConsole').info(text)
59+
getDebugConsoleLogger().info(text)
5960
log.verbose(`stdout: ${text}`)
6061
options?.onStdout?.(text, context)
6162
},
6263
onStderr: (text, context) => {
63-
getLogger('debugConsole').info(text)
64+
getDebugConsoleLogger().info(text)
6465
log.verbose(`stderr: ${text}`)
6566
options?.onStderr?.(text, context)
6667
},

0 commit comments

Comments
 (0)