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
Next Next commit
Checkpoint
  • Loading branch information
inlined committed Jun 15, 2022
commit df044259e1f23fdd5dd14dd24a7cadb8e211008d
14 changes: 2 additions & 12 deletions spec/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import { expect } from 'chai';

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

const SUPPORTS_STRUCTURED_LOGS =
parseInt(process.versions?.node?.split('.')?.[0] || '8', 10) >= 10;

describe(`logger (${
SUPPORTS_STRUCTURED_LOGS ? 'structured' : 'unstructured'
})`, () => {
describe('logger', () => {
const stdoutWrite = process.stdout.write.bind(process.stdout);
const stderrWrite = process.stderr.write.bind(process.stderr);
let lastOut: string;
Expand All @@ -30,12 +25,7 @@ describe(`logger (${
});

function expectOutput(last: string, entry: any) {
if (SUPPORTS_STRUCTURED_LOGS) {
return expect(JSON.parse(last.trim())).to.deep.eq(entry);
} else {
// legacy logging is not structured, but do a sanity check
return expect(last).to.include(entry.message);
}
return expect(JSON.parse(last.trim())).to.deep.eq(entry);
}

function expectStdout(entry: any) {
Expand Down
5 changes: 0 additions & 5 deletions src/logger/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// Determine if structured logs are supported (node >= 10). If something goes wrong,
// assume no since unstructured is safer.
/** @hidden */
export const SUPPORTS_STRUCTURED_LOGS =
parseInt(process.versions?.node?.split('.')?.[0] || '8', 10) >= 10;

// Map LogSeverity types to their equivalent `console.*` method.
/** @hidden */
Expand Down
13 changes: 6 additions & 7 deletions src/logger/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@
import { format } from 'util';
import {
CONSOLE_SEVERITY,
SUPPORTS_STRUCTURED_LOGS,
UNPATCHED_CONSOLE,
} from './common';

/** @hidden */
function patchedConsole(severity: string): (data: any, ...args: any[]) => void {
return function(data: any, ...args: any[]): void {
if (SUPPORTS_STRUCTURED_LOGS) {
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[severity]](
JSON.stringify({ severity, message: format(data, ...args) })
);
return;
let message = format(data, ...args);
if (severity === 'ERROR') {
message = new Error(message).stack;
}

UNPATCHED_CONSOLE[CONSOLE_SEVERITY[severity]](data, ...args);
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[severity]](
JSON.stringify({ severity, message })
);
};
}

Expand Down