Skip to content

fix(@angular/cli): restore console methods after logger completes#32828

Open
oBusk wants to merge 1 commit intoangular:mainfrom
oBusk:rebind-console-log
Open

fix(@angular/cli): restore console methods after logger completes#32828
oBusk wants to merge 1 commit intoangular:mainfrom
oBusk:rebind-console-log

Conversation

@oBusk
Copy link

@oBusk oBusk commented Mar 24, 2026

PR Checklist

Please check to confirm your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

console.log() when called after finally will be dropped. This could become an issue if code prints late like eslint which uses process.on("exit") to print timing information. This code will run after the logger has completed and will be dropped.

This means that TIMING=all ng lint does not work, eventhough eslint is seeing the var and collecting the data, but fails to print because console.log() is dead.

What is the new behavior?

Once the logger is completed, we reinsert console.log, console.info, console.warn and console.error so that any console logs that happens to trigger as the process is exiting, will still be printed to stdout.

Maybe there's a way to keep the logger living for longer, but since it's doing asyncronous things, I don't think we can clean it up in process.on('exit') or similiar? This fix at least prints to stdout as a best effort.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Ran into this issue by trying to print the timing information when using ng lint (angular-eslint), and realised that they werent doing anything weird, the console log was running, it was just disapparing. After some more tracking I realised that angular-cli was the one hijacking console.log(), so hoping to clean out this little tripwire.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the restoration of original console methods (log, info, warn, error) in the finally block to ensure consistent output from late consumers. The review suggests an improvement to make this restoration more robust and explicit by storing all original console methods in a single object to avoid assumptions and potential issues with chained assignments during restoration.

Comment on lines 42 to 44
const logInfo = console.log;
const logWarn = console.warn;
const logError = console.error;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To make the restoration of console methods more robust and explicit, consider saving all original console methods that are being patched into a single object. This avoids assumptions (like console.info being the same as console.log) and makes the code clearer about which methods are being backed up and restored.

You would then use this object in the finally block to restore the methods.

 const originalConsole = { log: console.log, info: console.info, warn: console.warn, error: console.error, };
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm i reused the logInfo, logError variables that were already defined for being used in the logger, felt unecessary to save them again. But I'm open to change it if reviewer agrees.

Comment on lines +116 to +118
console.log = console.info = logInfo;
console.warn = logWarn;
console.error = logError;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To correspond with storing the original console methods in an object, you can restore them here individually. This makes the restoration explicit and avoids any potential issues with chained assignments.

 console.log = originalConsole.log; console.info = originalConsole.info; console.warn = originalConsole.warn; console.error = originalConsole.error;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

1 participant