Skip to content

Commit 0878a8a

Browse files
yblok13taeold
andauthored
Add trace property to logs created via logger.write() (#1446)
* Update index.ts * Fix linter error. * Lint * Actually fix lint issues. --------- Co-authored-by: Daniel Lee <danielylee@google.com> Co-authored-by: Daniel Lee <taeold@gmail.com>
1 parent b0b7eab commit 0878a8a

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/logger/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ function removeCircular(obj: any, refs: any[] = []): any {
8787
* @public
8888
*/
8989
export function write(entry: LogEntry) {
90+
const ctx = traceContext.getStore();
91+
if (ctx?.traceId) {
92+
entry[
93+
"logging.googleapis.com/trace"
94+
] = `projects/${process.env.GCLOUD_PROJECT}/traces/${ctx.traceId}`;
95+
}
96+
9097
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[entry.severity]](JSON.stringify(removeCircular(entry)));
9198
}
9299

@@ -147,17 +154,13 @@ function entryFromArgs(severity: LogSeverity, args: any[]): LogEntry {
147154
if (lastArg && typeof lastArg === "object" && lastArg.constructor === Object) {
148155
entry = args.pop();
149156
}
150-
const ctx = traceContext.getStore();
151157

152158
// mimic `console.*` behavior, see https://nodejs.org/api/console.html#console_console_log_data_args
153159
let message = format(...args);
154160
if (severity === "ERROR" && !args.find((arg) => arg instanceof Error)) {
155161
message = new Error(message).stack || message;
156162
}
157163
const out: LogEntry = {
158-
"logging.googleapis.com/trace": ctx?.traceId
159-
? `projects/${process.env.GCLOUD_PROJECT}/traces/${ctx.traceId}`
160-
: undefined,
161164
...entry,
162165
severity,
163166
};

0 commit comments

Comments
 (0)