Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(shared): keep structured metadata in the default text log format (#18095)

Der Gerät378476050df3
diff --git a/packages/shared/src/server/logger.ts b/packages/shared/src/server/logger.ts
index 6131e6bdc6..edaac8d332 100644
--- a/packages/shared/src/server/logger.ts
+++ b/packages/shared/src/server/logger.ts
@@ -48,6 +48,39 @@ const severityFormat = function () {
})();
};
+// Metadata is arbitrary caller-supplied data: `Error` serialises to `{}`, and
+// circular or BigInt values make `JSON.stringify` throw. A logger that throws
+// destroys the diagnostic it was called to emit, so degrade to a marker.
+const stringifyMeta = (meta: Record<string, unknown>) => {
+ // Breaks cyclic `cause` chains: the replacer hands back a new object for
+ // every Error, so JSON.stringify's identity-based circular check never
+ // fires. An Error seen twice renders as "[Circular]" the second time.
+ const visitedErrors = new WeakSet<Error>();
+
+ const replacer = (_key: string, value: unknown) => {
+ if (value instanceof Error) {
+ if (visitedErrors.has(value)) return "[Circular]";
+ visitedErrors.add(value);
+ return {
+ ...value, // keeps fields an SDK attached to its own error type
+ name: value.name,
+ message: value.message,
+ stack: value.stack,
+ // `cause` and `errors` are non-enumerable, so the spread misses them
+ ...(value.cause === undefined ? {} : { cause: value.cause }),
+ ...(value instanceof AggregateError ? { errors: value.errors } : {}),
+ };
+ }
+ return typeof value === "bigint" ? value.toString() : value;
+ };
+
+ try {
+ return JSON.stringify(meta, replacer);
+ } catch {
+ return "[unserialisable log metadata]";
+ }
+};
+
const getWinstonLogger = (
nodeEnv: "development" | "production" | "test",
minLevel = "info",
@@ -57,8 +90,13 @@ const getWinstonLogger = (
winston.format.timestamp(),
winston.format.align(),
winston.format.printf((info) => {
- const logMessage = `${info.timestamp} ${info.level} ${info.message}`;
- return info.stack ? `${logMessage}\n${info.stack}` : logMessage;
+ // `splat` is winston's positional-args carrier, not output.
+ const { timestamp, level, message, stack, splat, ...meta } = info;
+ const rendered = Object.keys(meta).length
+ ? ` ${stringifyMeta(meta)}`
+ : "";
+ const logMessage = `${timestamp} ${level} ${message}${rendered}`;
+ return stack ? `${logMessage}\n${stack}` : logMessage;
}),
);