Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgrain committed Jan 17, 2025
1 parent 868accd commit 0e2b414
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
31 changes: 11 additions & 20 deletions packages/aws-cdk/lib/logging.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import * as util from 'util';
import * as chalk from 'chalk';
import { IoMessageLevel, IoMessage, CliIoHost, IoMessageSpecificCode, IoMessageCode, IoMessageCodeCategory, IoCodeLevel } from './toolkit/cli-io-host';
import { IoMessageLevel, IoMessage, CliIoHost, IoMessageSpecificCode, IoMessageCode, IoMessageCodeCategory, IoCodeLevel, levelPriority } from './toolkit/cli-io-host';

// Corking mechanism
let CORK_COUNTER = 0;
const logBuffer: IoMessage<any>[] = [];

const levelPriority: Record<IoMessageLevel, number> = {
error: 0,
warn: 1,
info: 2,
debug: 3,
trace: 4,
};

/**
* Executes a block of code with corked logging. All log messages during execution
* are buffered and only written when all nested cork blocks complete (when CORK_COUNTER reaches 0).
Expand All @@ -36,7 +28,7 @@ export async function withCorkedLogging<T>(block: () => Promise<T>): Promise<T>
}
}

interface LogOptions {
interface LogMessage {
/**
* The log level to use
*/
Expand All @@ -60,23 +52,22 @@ interface LogOptions {

/**
* Internal core logging function that writes messages through the CLI IO host.
* @param options Configuration options for the log message. See {@link LogOptions}
* @param msg Configuration options for the log message. See {@link LogMessage}
*/
function log(options: LogOptions) {
if (levelPriority[options.level] > levelPriority[CliIoHost.instance().logLevel]) {
return;
}

function log(msg: LogMessage) {
const ioMessage: IoMessage<undefined> = {
level: options.level,
message: options.message,
forceStdout: options.forceStdout,
level: msg.level,
message: msg.message,
forceStdout: msg.forceStdout,
time: new Date(),
action: CliIoHost.instance().currentAction,
code: options.code,
code: msg.code,
};

if (CORK_COUNTER > 0) {
if (levelPriority[msg.level] > levelPriority[CliIoHost.instance().logLevel]) {
return;
}
logBuffer.push(ioMessage);
return;
}
Expand Down
47 changes: 29 additions & 18 deletions packages/aws-cdk/lib/toolkit/cli-io-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ export interface IoRequest<T, U> extends IoMessage<T> {

export type IoMessageLevel = 'error' | 'warn' | 'info' | 'debug' | 'trace';

export const levelPriority: Record<IoMessageLevel, number> = {
error: 0,
warn: 1,
info: 2,
debug: 3,
trace: 4,
};

/**
* The current action being performed by the CLI. 'none' represents the absence of an action.
*/
Expand Down Expand Up @@ -167,22 +175,6 @@ export class CliIoHost implements IIoHost {
*/
private static _instance: CliIoHost | undefined;

/**
* Determines which output stream to use based on log level and configuration.
*/
private static getStream(level: IoMessageLevel, forceStdout: boolean) {
// For legacy purposes all log streams are written to stderr by default, unless
// specified otherwise, by passing `forceStdout`, which is used by the `data()` logging function, or
// if the CDK is running in a CI environment. This is because some CI environments will immediately
// fail if stderr is written to. In these cases, we detect if we are in a CI environment and
// write all messages to stdout instead.
if (forceStdout) {
return process.stdout;
}
if (level == 'error') return process.stderr;
return CliIoHost.instance().isCI ? process.stdout : process.stderr;
}

private _currentAction: ToolkitAction;
private _isCI: boolean;
private _isTTY: boolean;
Expand Down Expand Up @@ -276,9 +268,12 @@ export class CliIoHost implements IIoHost {
return this._internalIoHost.notify(msg);
}

const output = this.formatMessage(msg);
if (levelPriority[msg.level] > levelPriority[this.logLevel]) {
return;
}

const stream = CliIoHost.getStream(msg.level, msg.forceStdout ?? false);
const output = this.formatMessage(msg);
const stream = this.stream(msg.level, msg.forceStdout ?? false);

return new Promise((resolve, reject) => {
stream.write(output, (err) => {
Expand All @@ -291,6 +286,22 @@ export class CliIoHost implements IIoHost {
});
}

/**
* Determines which output stream to use based on log level and configuration.
*/
private stream(level: IoMessageLevel, forceStdout: boolean) {
// For legacy purposes all log streams are written to stderr by default, unless
// specified otherwise, by passing `forceStdout`, which is used by the `data()` logging function, or
// if the CDK is running in a CI environment. This is because some CI environments will immediately
// fail if stderr is written to. In these cases, we detect if we are in a CI environment and
// write all messages to stdout instead.
if (forceStdout) {
return process.stdout;
}
if (level == 'error') return process.stderr;
return CliIoHost.instance().isCI ? process.stdout : process.stderr;
}

/**
* Notifies the host of a message that requires a response.
*
Expand Down
4 changes: 3 additions & 1 deletion packages/aws-cdk/test/toolkit/cli-io-host.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as chalk from 'chalk';
import { CliIoHost, IoMessage } from '../../lib/toolkit/cli-io-host';

const ioHost = CliIoHost.instance();
const ioHost = CliIoHost.instance({
logLevel: 'trace',
});

describe('CliIoHost', () => {
let mockStdout: jest.Mock;
Expand Down

0 comments on commit 0e2b414

Please sign in to comment.