diff --git a/src/logger/index.ts b/src/logger/index.ts index 9fc40ef88..a9f2aec9e 100644 --- a/src/logger/index.ts +++ b/src/logger/index.ts @@ -87,6 +87,13 @@ function removeCircular(obj: any, refs: any[] = []): any { * @public */ export function write(entry: LogEntry) { + const ctx = traceContext.getStore(); + if (ctx?.traceId) { + entry[ + "logging.googleapis.com/trace" + ] = `projects/${process.env.GCLOUD_PROJECT}/traces/${ctx.traceId}`; + } + UNPATCHED_CONSOLE[CONSOLE_SEVERITY[entry.severity]](JSON.stringify(removeCircular(entry))); } @@ -147,7 +154,6 @@ function entryFromArgs(severity: LogSeverity, args: any[]): LogEntry { if (lastArg && typeof lastArg === "object" && lastArg.constructor === Object) { entry = args.pop(); } - const ctx = traceContext.getStore(); // mimic `console.*` behavior, see https://nodejs.org/api/console.html#console_console_log_data_args let message = format(...args); @@ -155,9 +161,6 @@ function entryFromArgs(severity: LogSeverity, args: any[]): LogEntry { message = new Error(message).stack || message; } const out: LogEntry = { - "logging.googleapis.com/trace": ctx?.traceId - ? `projects/${process.env.GCLOUD_PROJECT}/traces/${ctx.traceId}` - : undefined, ...entry, severity, };