|
22 | 22 |
|
23 | 23 | import { format } from 'util';
|
24 | 24 |
|
25 |
| -import { |
26 |
| - CONSOLE_SEVERITY, |
27 |
| - SUPPORTS_STRUCTURED_LOGS, |
28 |
| - UNPATCHED_CONSOLE, |
29 |
| -} from './common'; |
| 25 | +import { CONSOLE_SEVERITY, UNPATCHED_CONSOLE } from './common'; |
30 | 26 |
|
31 | 27 | /**
|
32 | 28 | * `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 {
|
90 | 86 | * @public
|
91 | 87 | */
|
92 | 88 | 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 | + ); |
117 | 92 | }
|
118 | 93 |
|
119 | 94 | /**
|
@@ -173,9 +148,10 @@ function entryFromArgs(severity: LogSeverity, args: any[]): LogEntry {
|
173 | 148 | if (lastArg && typeof lastArg == 'object' && lastArg.constructor == Object) {
|
174 | 149 | entry = args.pop();
|
175 | 150 | }
|
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 }; |
181 | 157 | }
|
0 commit comments