Skip to content
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

Error logs always show up in Cloud Error Reporting #1141

Merged
merged 4 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
Make logging.error show up in Cloud Error Reporting
  • Loading branch information
inlined committed Jun 15, 2022
commit 768366a1310aab406e833219c921787045f595b6
2 changes: 1 addition & 1 deletion src/logger/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function patchedConsole(severity: string): (data: any, ...args: any[]) => void {
return function(data: any, ...args: any[]): void {
let message = format(data, ...args);
if (severity === 'ERROR') {
message = new Error(message).stack;
message = new Error(message).stack || message;
}

UNPATCHED_CONSOLE[CONSOLE_SEVERITY[severity]](
Expand Down
39 changes: 9 additions & 30 deletions src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { format } from 'util';

import {
CONSOLE_SEVERITY,
SUPPORTS_STRUCTURED_LOGS,
UNPATCHED_CONSOLE,
} from './common';

Expand Down Expand Up @@ -90,30 +89,9 @@ function removeCircular(obj: any, refs: any[] = []): any {
* @public
*/
export function write(entry: LogEntry) {
if (SUPPORTS_STRUCTURED_LOGS) {
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[entry.severity]](
JSON.stringify(removeCircular(entry))
);
return;
}

let message = entry.message || '';
const jsonPayload: { [key: string]: any } = {};
let jsonKeyCount = 0;
for (const k in entry) {
if (!['severity', 'message'].includes(k)) {
jsonKeyCount++;
jsonPayload[k] = entry[k];
}
}
if (jsonKeyCount > 0) {
message = `${message} ${JSON.stringify(
removeCircular(jsonPayload),
null,
2
)}`;
}
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[entry.severity]](message);
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[entry.severity]](
JSON.stringify(removeCircular(entry))
);
}

/**
Expand Down Expand Up @@ -173,9 +151,10 @@ function entryFromArgs(severity: LogSeverity, args: any[]): LogEntry {
if (lastArg && typeof lastArg == 'object' && lastArg.constructor == Object) {
entry = args.pop();
}
return Object.assign({}, entry, {
severity,
// mimic `console.*` behavior, see https://nodejs.org/api/console.html#console_console_log_data_args
message: format.apply(null, args),
});
// mimic `console.*` behavior, see https://nodejs.org/api/console.html#console_console_log_data_args
let message = format.apply(null, args);
if (severity === "ERROR") {
message = new Error(message).stack || message;
}
return { ...entry, severity, message };
}