Skip to content

Commit e5f8ece

Browse files
committed
chore(rivetkit): clean up logging configuration
1 parent ad3bcfe commit e5f8ece

File tree

7 files changed

+79
-27
lines changed

7 files changed

+79
-27
lines changed

rivetkit-typescript/packages/cloudflare-workers/tests/driver-tests.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ async function setupProject(projectPath: string) {
207207
LOG_LEVEL: "DEBUG",
208208
LOG_TARGET: "1",
209209
LOG_TIMESTAMP: "1",
210-
_RIVETKIT_ERROR_STACK: "1",
211-
_RIVETKIT_LOG_MESSAGE: "1",
210+
LOG_ERROR_STACK: "1",
211+
LOG_MESSAGE: "1",
212212
},
213213
};
214214
await fs.writeFile(

rivetkit-typescript/packages/rivetkit/src/client/actor-conn.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
UniversalEventSource,
1717
UniversalMessageEvent,
1818
} from "@/common/eventsource-interface";
19+
import { isLogMessageEnabled } from "@/common/log-config";
1920
import { assertUnreachable, stringifyError } from "@/common/utils";
2021
import type { UniversalWebSocket } from "@/common/websocket-interface";
2122
import type { ManagerDriver } from "@/driver-helpers/mod";
@@ -39,7 +40,6 @@ import {
3940
} from "@/serde";
4041
import {
4142
bufferToArrayBuffer,
42-
getEnvUniversal,
4343
httpUserAgent,
4444
promiseWithResolvers,
4545
} from "@/utils";
@@ -418,7 +418,7 @@ enc
418418

419419
const response = await this.#parseMessage(data as ConnMessage);
420420
logger().trace(
421-
getEnvUniversal("_RIVETKIT_LOG_MESSAGE")
421+
isLogMessageEnabled()
422422
? {
423423
msg: "parsed message",
424424
message:
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { getEnvUniversal } from "@/utils";
2+
3+
/**
4+
* Logging configuration functions.
5+
*
6+
* These functions provide access to logging-related environment variables.
7+
*/
8+
9+
/**
10+
* Get log level (e.g., "debug", "info", "warn", "error").
11+
* Defaults to "warn" if not set.
12+
*/
13+
export function getLogLevelEnv(): string | undefined {
14+
return getEnvUniversal("LOG_LEVEL");
15+
}
16+
17+
/**
18+
* Check if log target should be included in log output.
19+
* Returns true when LOG_TARGET=1.
20+
*/
21+
export function isLogTargetEnabled(): boolean {
22+
return getEnvUniversal("LOG_TARGET") === "1";
23+
}
24+
25+
/**
26+
* Check if timestamps should be included in log output.
27+
* Returns true when LOG_TIMESTAMP=1.
28+
*/
29+
export function isLogTimestampEnabled(): boolean {
30+
return getEnvUniversal("LOG_TIMESTAMP") === "1";
31+
}
32+
33+
/**
34+
* Check if detailed message logging is enabled for debugging.
35+
* Returns true when LOG_MESSAGE=1.
36+
*/
37+
export function isLogMessageEnabled(): boolean {
38+
return getEnvUniversal("LOG_MESSAGE") === "1";
39+
}
40+
41+
/**
42+
* Check if stack traces should be included in error stringification.
43+
* Returns true when LOG_ERROR_STACK=1.
44+
*/
45+
export function isErrorStackEnabled(): boolean {
46+
return getEnvUniversal("LOG_ERROR_STACK") === "1";
47+
}
48+
49+
/**
50+
* Check if HTTP headers should be logged.
51+
* Returns true when LOG_HEADERS=1.
52+
*/
53+
export function isLogHeadersEnabled(): boolean {
54+
return getEnvUniversal("LOG_HEADERS") === "1";
55+
}

rivetkit-typescript/packages/rivetkit/src/common/log.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ import {
55
stdTimeFunctions,
66
} from "pino";
77
import { z } from "zod";
8-
import { getEnvUniversal } from "@/utils";
98
import {
109
castToLogValue,
1110
formatTimestamp,
1211
LOGGER_CONFIG,
1312
stringify,
1413
} from "./logfmt";
14+
import {
15+
getLogLevelEnv,
16+
isLogTargetEnabled,
17+
isLogTimestampEnabled,
18+
} from "./log-config";
1519

1620
export type { Logger } from "pino";
1721

@@ -43,9 +47,7 @@ export function getPinoLevel(logLevel?: LogLevel): LevelWithSilent {
4347
return configuredLogLevel;
4448
}
4549

46-
const raw = (getEnvUniversal("LOG_LEVEL") || "warn")
47-
.toString()
48-
.toLowerCase();
50+
const raw = (getLogLevelEnv() || "warn").toString().toLowerCase();
4951

5052
const parsed = LogLevelSchema.safeParse(raw);
5153
if (parsed.success) {
@@ -57,7 +59,7 @@ export function getPinoLevel(logLevel?: LogLevel): LevelWithSilent {
5759
}
5860

5961
export function getIncludeTarget(): boolean {
60-
return getEnvUniversal("LOG_TARGET") === "1";
62+
return isLogTargetEnabled();
6163
}
6264

6365
/**
@@ -73,7 +75,7 @@ function customWrite(level: string, o: any) {
7375
const entries: any = {};
7476

7577
// Add timestamp if enabled
76-
if (getEnvUniversal("LOG_TIMESTAMP") === "1" && o.time) {
78+
if (isLogTimestampEnabled() && o.time) {
7779
const date = typeof o.time === "number" ? new Date(o.time) : new Date();
7880
entries.ts = formatTimestamp(date);
7981
}
@@ -131,10 +133,9 @@ export async function configureDefaultLogger(
131133
return { level: number };
132134
},
133135
},
134-
timestamp:
135-
getEnvUniversal("LOG_TIMESTAMP") === "1"
136-
? stdTimeFunctions.epochTime
137-
: false,
136+
timestamp: isLogTimestampEnabled()
137+
? stdTimeFunctions.epochTime
138+
: false,
138139
browser: {
139140
write: {
140141
fatal: customWrite.bind(null, "fatal"),
@@ -158,10 +159,7 @@ export async function configureDefaultLogger(
158159
60: "fatal",
159160
};
160161
const levelName = levelMap[level] || "info";
161-
const time =
162-
getEnvUniversal("LOG_TIMESTAMP") === "1"
163-
? Date.now()
164-
: undefined;
162+
const time = isLogTimestampEnabled() ? Date.now() : undefined;
165163

166164
// Get bindings from the logger instance (child logger fields)
167165
const bindings = (this as any).bindings?.() || {};

rivetkit-typescript/packages/rivetkit/src/common/router.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import {
1717
HttpResponseErrorSchema,
1818
} from "@/schemas/client-protocol-zod/mod";
1919
import { encodingIsBinary, serializeWithEncoding } from "@/serde";
20-
import { bufferToArrayBuffer, getEnvUniversal, VERSION } from "@/utils";
20+
import { bufferToArrayBuffer, VERSION } from "@/utils";
2121
import { getLogger, type Logger } from "./log";
22+
import { isLogHeadersEnabled } from "./log-config";
2223
import { deconstructError, stringifyError } from "./utils";
2324

2425
export function logger() {
@@ -43,7 +44,7 @@ export function loggerMiddleware(logger: Logger) {
4344
reqSize: c.req.header("content-length"),
4445
resSize: c.res.headers.get("content-length"),
4546
userAgent: c.req.header("user-agent"),
46-
...(getEnvUniversal("_RIVET_LOG_HEADERS")
47+
...(isLogHeadersEnabled()
4748
? { allHeaders: JSON.stringify(c.req.header()) }
4849
: {}),
4950
});

rivetkit-typescript/packages/rivetkit/src/common/utils.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { Next } from "hono";
22
import type { ContentfulStatusCode } from "hono/utils/http-status";
33
import * as errors from "@/actor/errors";
4-
import { EXTRA_ERROR_LOG, getEnvUniversal, VERSION } from "@/utils";
4+
import { EXTRA_ERROR_LOG, VERSION } from "@/utils";
5+
import { isErrorStackEnabled } from "./log-config";
56
import type { Logger } from "./log";
67

78
export function assertUnreachable(x: never): never {
@@ -298,10 +299,7 @@ export function deconstructError(
298299

299300
export function stringifyError(error: unknown): string {
300301
if (error instanceof Error) {
301-
if (
302-
typeof process !== "undefined" &&
303-
getEnvUniversal("_RIVETKIT_ERROR_STACK") === "1"
304-
) {
302+
if (typeof process !== "undefined" && isErrorStackEnabled()) {
305303
return `${error.name}: ${error.message}${error.stack ? `\n${error.stack}` : ""}`;
306304
} else {
307305
return `${error.name}: ${error.message}`;

vitest.base.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export default {
1414
LOG_LEVEL: "DEBUG",
1515
LOG_TARGET: "1",
1616
LOG_TIMESTAMP: "1",
17-
_RIVETKIT_ERROR_STACK: "1",
18-
_RIVETKIT_LOG_MESSAGE: "1",
17+
LOG_ERROR_STACK: "1",
18+
LOG_MESSAGE: "1",
1919
},
2020
},
2121
} satisfies ViteUserConfig;

0 commit comments

Comments
 (0)