Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions spec/logger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from "chai";

import * as logger from "../src/logger";
import { setGlobalOptions } from "../src/v2";

describe("logger", () => {
const stdoutWrite = process.stdout.write.bind(process.stdout);
Expand Down Expand Up @@ -218,4 +219,26 @@ describe("logger", () => {
});
});
});

describe("error logging stacktrace", () => {
it("should include stacktrace in error logs", () => {
const message = "Test error with stacktrace";
logger.error(message);
const messageOutput = JSON.parse(lastErr.trim()).message;

expect(messageOutput).to.include(`Error: ${message}`);
});

it("when disableErrorLoggingTraceback is set to true, should not log stacktrace", () => {
const message = "Test error with traceback disabled";
setGlobalOptions({
disableErrorLoggingTraceback: true,
});
logger.error(message);
expectStderr({
severity: "ERROR",
message: message,
});
});
});
});
7 changes: 6 additions & 1 deletion src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { format } from "util";
import { traceContext } from "../common/trace";

import { CONSOLE_SEVERITY, UNPATCHED_CONSOLE } from "./common";
import { getGlobalOptions } from "../v2/options";

/**
* `LogSeverity` indicates the detailed severity of the log entry. See [LogSeverity](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity).
Expand Down Expand Up @@ -158,7 +159,11 @@ function entryFromArgs(severity: LogSeverity, args: any[]): LogEntry {

// mimic `console.*` behavior, see https://nodejs.org/api/console.html#console_console_log_data_args
let message = format(...args);
if (severity === "ERROR" && !args.find((arg) => arg instanceof Error)) {
if (
severity === "ERROR" &&
!getGlobalOptions().disableErrorLoggingTraceback &&
!args.find((arg) => arg instanceof Error)
) {
message = new Error(message).stack || message;
}
const out: LogEntry = {
Expand Down
9 changes: 9 additions & 0 deletions src/v2/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ export interface GlobalOptions {
* may inadvertently be wiped out.
*/
preserveExternalChanges?: boolean;

/**
* Controls whether error logging should include the traceback of the error automatically.
* Defaults to false.
*
* @remarks
* When true, the error message will include not include the traceback of the error.
*/
disableErrorLoggingTraceback?: boolean;
}

let globalOptions: GlobalOptions | undefined;
Expand Down
Loading