Skip to content

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

fix(logging): add severity field to JSON logs (#17606)

Steffen Reuther3b50a4450390
diff --git a/packages/shared/src/server/logger.ts b/packages/shared/src/server/logger.ts
index 664be2a1af..6131e6bdc6 100644
--- a/packages/shared/src/server/logger.ts
+++ b/packages/shared/src/server/logger.ts
@@ -24,6 +24,30 @@ const tracingFormat = function () {
})();
};
+/**
+ * Log collectors disagree on which field carries the level. GCP Cloud Logging
+ * only reads `severity`, and only its own LogSeverity names, so a JSON line
+ * that carries the level in `level` is indexed as DEFAULT there. Datadog and
+ * Loki read `level` as well, so emitting both fields makes one line sort
+ * correctly everywhere.
+ */
+const gcpSeverityByLevel: Record<string, string> = {
+ error: "ERROR",
+ warn: "WARNING",
+ info: "INFO",
+ http: "INFO",
+ verbose: "DEBUG",
+ debug: "DEBUG",
+ silly: "DEBUG",
+};
+
+const severityFormat = function () {
+ return winston.format((info) => {
+ info.severity = gcpSeverityByLevel[info.level] ?? "DEFAULT";
+ return info;
+ })();
+};
+
const getWinstonLogger = (
nodeEnv: "development" | "production" | "test",
minLevel = "info",
@@ -42,6 +66,7 @@ const getWinstonLogger = (
winston.format.errors({ stack: true }),
winston.format.timestamp(),
tracingFormat(),
+ severityFormat(),
winston.format.json(),
);