Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 35 additions & 0 deletions rivetkit-typescript/packages/rivetkit/src/common/gcp-log-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { LoggerOptions } from "pino";

// Lightweight version of @google-cloud/pino-logging-gcp-config.
// This only maps pino levels to GCP severity for compute environments.

const PINO_TO_GCP_SEVERITY: Record<string, string> = {
trace: "DEBUG",
debug: "DEBUG",
info: "INFO",
warn: "WARNING",
error: "ERROR",
fatal: "CRITICAL",
};

export function pinoLevelToGcpSeverity(
pinoSeverityLabel: string,
pinoSeverityLevel: number,
): Record<string, unknown> {
const severity =
PINO_TO_GCP_SEVERITY[pinoSeverityLabel] ?? "INFO";
return { severity, level: pinoSeverityLevel };
}

export function createGcpLoggingPinoConfig(
pinoLoggerOptionsMixin?: LoggerOptions,
): LoggerOptions {
const formattersMixin = pinoLoggerOptionsMixin?.formatters;
return {
...pinoLoggerOptionsMixin,
formatters: {
...formattersMixin,
level: pinoLevelToGcpSeverity,
},
};
}
21 changes: 20 additions & 1 deletion rivetkit-typescript/packages/rivetkit/src/common/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import {
stdTimeFunctions,
} from "pino";
import { z } from "zod/v4";
import { getLogLevel, getLogTarget, getLogTimestamp } from "@/utils/env-vars";
import {
getLogLevel,
getLogTarget,
getLogTimestamp,
getRivetComputeEnabled,
} from "@/utils/env-vars";
import {
castToLogValue,
formatTimestamp,
LOGGER_CONFIG,
stringify,
} from "./logfmt";
import { createGcpLoggingPinoConfig } from "./gcp-log-helper";

export type { Logger } from "pino";

Expand Down Expand Up @@ -116,6 +122,19 @@ export function configureDefaultLogger(logLevel?: LogLevel) {
configuredLogLevel = logLevel;
}

if (getRivetComputeEnabled() && typeof window === "undefined") {
baseLogger = pino(
createGcpLoggingPinoConfig({
level: getPinoLevel(logLevel),
messageKey: "msg",
base: {},
timestamp: getLogTimestamp() ? stdTimeFunctions.epochTime : false,
}),
);
loggerCache.clear();
return;
}

baseLogger = pino({
level: getPinoLevel(logLevel),
messageKey: "msg",
Expand Down
4 changes: 4 additions & 0 deletions rivetkit-typescript/packages/rivetkit/src/utils/env-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export const getRivetkitInspectorDisable = (): boolean =>
export const getRivetkitStoragePath = (): string | undefined =>
getEnvUniversal("RIVETKIT_STORAGE_PATH");

// Internal compute marker for backend logging. Intentionally undocumented for now.
export const getRivetComputeEnabled = (): boolean =>
getEnvUniversal("_RIVET_COMPUTE_ENABLED") === "1";

// Logging configuration
// DEPRECATED: LOG_LEVEL will be removed in a future version
export const getLogLevel = (): string | undefined =>
Expand Down
Loading