-
Notifications
You must be signed in to change notification settings - Fork 233
fix(logger): do not synthesize error stack on console.error in logger… #1948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,12 +24,14 @@ import { format } from "util"; | |
| import { CONSOLE_SEVERITY, UNPATCHED_CONSOLE } from "./common"; | ||
|
|
||
| /** @hidden */ | ||
| function patchedConsole(severity: string): (data: any, ...args: any[]) => void { | ||
| return function (data: any, ...args: any[]): void { | ||
| let message = format(data, ...args); | ||
| if (severity === "ERROR") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there any cases where we still want this behavior if the message was from an actual stack trace or something? I'm not sure if in addition to fixing a real problem we're regressing in some cases where we actually wanted this behavior. Someone wrote it this way for a reason the first time but I don't know why. I'd be curious if you did any research into this and what you found.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked into the git history to see why it was written this way. logger/compat was originally added in v3 (PR #701) purely as a drop-in shim to output structured JSON without any stack synthesis. In v4 (PR #1161), when the main SDK logger (functions.logger.error) was updated to synthesize stacks for Cloud Error Reporting, that line was copied over to compat.ts as well (and without the instanceof Error check). While synthesizing a stack makes sense when someone explicitly calls functions.logger.error("..."), doing it in console.error causes Node runtime warnings (like MaxListenersExceededWarning from connection pools) to get decorated with a fake stack trace and misclassified as application crashes in Cloud Error Reporting. It also double-wrapped actual Error objects passed to console.error(err). Taking this out doesn't regress intended behavior: console.error(err) with real Error objects still outputs the genuine stack trace through util.format, string logs will still be categorized as severity: "ERROR", and the main SDK logger (src/logger/index.ts) remains untouched. This just restores logger/compat to its original intended behavior.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. Thanks for digging a bit more. I'm convinced. |
||
| message = new Error(message).stack || message; | ||
| } | ||
| function patchedConsole(severity: string): (...args: unknown[]) => void { | ||
| return function (...args: unknown[]): void { | ||
| // Format arguments matching standard Node console.* behavior. | ||
| // Unlike logger.error, we intentionally do NOT synthesize an Error stack for | ||
| // console.error so that Node.js runtime warnings (e.g. MaxListenersExceededWarning) | ||
| // and standard console.error string logs are not misclassified as unhandled exceptions | ||
| // in Cloud Error Reporting. | ||
| const message = format(...args); | ||
|
|
||
| UNPATCHED_CONSOLE[CONSOLE_SEVERITY[severity]](JSON.stringify({ severity, message })); | ||
| }; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.