Skip to content

Commit ea5edf9

Browse files
authored
Error logs always show up in Cloud Error Reporting (#1141)
* Checkpoint * Make logging.error show up in Cloud Error Reporting * Make logging.error show up in Cloud Error Reporting * Lint fixes
1 parent c06b4fe commit ea5edf9

File tree

4 files changed

+19
-63
lines changed

4 files changed

+19
-63
lines changed

spec/logger.spec.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ import { expect } from 'chai';
22

33
import * as logger from '../src/logger';
44

5-
const SUPPORTS_STRUCTURED_LOGS =
6-
parseInt(process.versions?.node?.split('.')?.[0] || '8', 10) >= 10;
7-
8-
describe(`logger (${
9-
SUPPORTS_STRUCTURED_LOGS ? 'structured' : 'unstructured'
10-
})`, () => {
5+
describe('logger', () => {
116
const stdoutWrite = process.stdout.write.bind(process.stdout);
127
const stderrWrite = process.stderr.write.bind(process.stderr);
138
let lastOut: string;
@@ -30,12 +25,7 @@ describe(`logger (${
3025
});
3126

3227
function expectOutput(last: string, entry: any) {
33-
if (SUPPORTS_STRUCTURED_LOGS) {
34-
return expect(JSON.parse(last.trim())).to.deep.eq(entry);
35-
} else {
36-
// legacy logging is not structured, but do a sanity check
37-
return expect(last).to.include(entry.message);
38-
}
28+
return expect(JSON.parse(last.trim())).to.deep.eq(entry);
3929
}
4030

4131
function expectStdout(entry: any) {

src/logger/common.ts

-6
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23-
// Determine if structured logs are supported (node >= 10). If something goes wrong,
24-
// assume no since unstructured is safer.
25-
/** @hidden */
26-
export const SUPPORTS_STRUCTURED_LOGS =
27-
parseInt(process.versions?.node?.split('.')?.[0] || '8', 10) >= 10;
28-
2923
// Map LogSeverity types to their equivalent `console.*` method.
3024
/** @hidden */
3125
export const CONSOLE_SEVERITY: {

src/logger/compat.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,19 @@
2121
// SOFTWARE.
2222

2323
import { format } from 'util';
24-
import {
25-
CONSOLE_SEVERITY,
26-
SUPPORTS_STRUCTURED_LOGS,
27-
UNPATCHED_CONSOLE,
28-
} from './common';
24+
import { CONSOLE_SEVERITY, UNPATCHED_CONSOLE } from './common';
2925

3026
/** @hidden */
3127
function patchedConsole(severity: string): (data: any, ...args: any[]) => void {
3228
return function(data: any, ...args: any[]): void {
33-
if (SUPPORTS_STRUCTURED_LOGS) {
34-
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[severity]](
35-
JSON.stringify({ severity, message: format(data, ...args) })
36-
);
37-
return;
29+
let message = format(data, ...args);
30+
if (severity === 'ERROR') {
31+
message = new Error(message).stack || message;
3832
}
3933

40-
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[severity]](data, ...args);
34+
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[severity]](
35+
JSON.stringify({ severity, message })
36+
);
4137
};
4238
}
4339

src/logger/index.ts

+10-34
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@
2222

2323
import { format } from 'util';
2424

25-
import {
26-
CONSOLE_SEVERITY,
27-
SUPPORTS_STRUCTURED_LOGS,
28-
UNPATCHED_CONSOLE,
29-
} from './common';
25+
import { CONSOLE_SEVERITY, UNPATCHED_CONSOLE } from './common';
3026

3127
/**
3228
* `LogSeverity` indicates the detailed severity of the log entry. See [LogSeverity](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity).
@@ -90,30 +86,9 @@ function removeCircular(obj: any, refs: any[] = []): any {
9086
* @public
9187
*/
9288
export function write(entry: LogEntry) {
93-
if (SUPPORTS_STRUCTURED_LOGS) {
94-
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[entry.severity]](
95-
JSON.stringify(removeCircular(entry))
96-
);
97-
return;
98-
}
99-
100-
let message = entry.message || '';
101-
const jsonPayload: { [key: string]: any } = {};
102-
let jsonKeyCount = 0;
103-
for (const k in entry) {
104-
if (!['severity', 'message'].includes(k)) {
105-
jsonKeyCount++;
106-
jsonPayload[k] = entry[k];
107-
}
108-
}
109-
if (jsonKeyCount > 0) {
110-
message = `${message} ${JSON.stringify(
111-
removeCircular(jsonPayload),
112-
null,
113-
2
114-
)}`;
115-
}
116-
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[entry.severity]](message);
89+
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[entry.severity]](
90+
JSON.stringify(removeCircular(entry))
91+
);
11792
}
11893

11994
/**
@@ -173,9 +148,10 @@ function entryFromArgs(severity: LogSeverity, args: any[]): LogEntry {
173148
if (lastArg && typeof lastArg == 'object' && lastArg.constructor == Object) {
174149
entry = args.pop();
175150
}
176-
return Object.assign({}, entry, {
177-
severity,
178-
// mimic `console.*` behavior, see https://nodejs.org/api/console.html#console_console_log_data_args
179-
message: format.apply(null, args),
180-
});
151+
// mimic `console.*` behavior, see https://nodejs.org/api/console.html#console_console_log_data_args
152+
let message = format.apply(null, args);
153+
if (severity === 'ERROR' && !args.find((arg) => arg instanceof Error)) {
154+
message = new Error(message).stack || message;
155+
}
156+
return { ...entry, severity, message };
181157
}

0 commit comments

Comments
 (0)