fix(@angular/cli): restore console methods after logger completes#32828
fix(@angular/cli): restore console methods after logger completes#32828oBusk wants to merge 1 commit intoangular:mainfrom
Conversation
There was a problem hiding this comment.
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.
| const logInfo = console.log; | ||
| const logWarn = console.warn; | ||
| const logError = console.error; |
There was a problem hiding this comment.
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, };There was a problem hiding this comment.
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.
| console.log = console.info = logInfo; | ||
| console.warn = logWarn; | ||
| console.error = logError; |
There was a problem hiding this comment.
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;
PR Checklist
Please check to confirm your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: N/A
console.log()when called afterfinallywill be dropped. This could become an issue if code prints late likeeslintwhich usesprocess.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 lintdoes not work, eventhougheslintis seeing the var and collecting the data, but fails to print becauseconsole.log()is dead.What is the new behavior?
Once the logger is completed, we reinsert
console.log,console.info,console.warnandconsole.errorso 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?
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.