Skip to content

Commit 25fdb27

Browse files
committed
chore(rivetkit): clean up logging configuration
1 parent f7c3edc commit 25fdb27

File tree

9 files changed

+57
-69
lines changed

9 files changed

+57
-69
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,11 @@ async function setupProject(projectPath: string) {
204204
enabled: true,
205205
},
206206
vars: {
207-
LOG_LEVEL: "DEBUG",
208-
LOG_TARGET: "1",
209-
LOG_TIMESTAMP: "1",
210-
_RIVETKIT_ERROR_STACK: "1",
211-
_RIVETKIT_LOG_MESSAGE: "1",
207+
RIVETKIT_LOG_LEVEL: "DEBUG",
208+
RIVETKIT_LOG_TARGET: "1",
209+
RIVETKIT_LOG_TIMESTAMP: "1",
210+
RIVETKIT_LOG_ERROR_STACK: "1",
211+
RIVETKIT_LOG_MESSAGE: "1",
212212
},
213213
};
214214
await fs.writeFile(

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

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,7 @@ import type { CloseEvent } from "ws";
55
import type { AnyActorDefinition } from "@/actor/definition";
66
import { inputDataToBuffer } from "@/actor/protocol/old";
77
import { type Encoding, jsonStringifyCompat } from "@/actor/protocol/serde";
8-
import {
9-
HEADER_CONN_PARAMS,
10-
HEADER_ENCODING,
11-
PATH_CONNECT,
12-
} from "@/common/actor-router-consts";
13-
import { importEventSource } from "@/common/eventsource";
14-
import type {
15-
UniversalErrorEvent,
16-
UniversalEventSource,
17-
UniversalMessageEvent,
18-
} from "@/common/eventsource-interface";
8+
import { PATH_CONNECT } from "@/common/actor-router-consts";
199
import { assertUnreachable, stringifyError } from "@/common/utils";
2010
import type { UniversalWebSocket } from "@/common/websocket-interface";
2111
import type { ManagerDriver } from "@/driver-helpers/mod";
@@ -32,17 +22,9 @@ import {
3222
type ToServer as ToServerJson,
3323
ToServerSchema,
3424
} from "@/schemas/client-protocol-zod/mod";
35-
import {
36-
deserializeWithEncoding,
37-
encodingIsBinary,
38-
serializeWithEncoding,
39-
} from "@/serde";
40-
import {
41-
bufferToArrayBuffer,
42-
httpUserAgent,
43-
promiseWithResolvers,
44-
} from "@/utils";
45-
import { getRivetkitLogMessage } from "@/utils/env-vars";
25+
import { deserializeWithEncoding, serializeWithEncoding } from "@/serde";
26+
import { bufferToArrayBuffer, promiseWithResolvers } from "@/utils";
27+
import { getLogMessage } from "@/utils/env-vars";
4628
import type { ActorDefinitionActions } from "./actor-common";
4729
import { queryActor } from "./actor-query";
4830
import { ACTOR_CONNS_SYMBOL, type ClientRaw } from "./client";
@@ -62,7 +44,11 @@ import {
6244
* - `"connected"`: Connection is active
6345
* - `"disconnected"`: Connection was lost, will auto-reconnect
6446
*/
65-
export type ActorConnStatus = "idle" | "connecting" | "connected" | "disconnected";
47+
export type ActorConnStatus =
48+
| "idle"
49+
| "connecting"
50+
| "connected"
51+
| "disconnected";
6652

6753
interface ActionInFlight {
6854
name: string;
@@ -298,8 +284,7 @@ enc
298284

299285
// Notify close handlers (only if transitioning from Connected to Disconnected or Idle)
300286
if (
301-
(status === "disconnected" ||
302-
status === "idle") &&
287+
(status === "disconnected" || status === "idle") &&
303288
prevStatus === "connected"
304289
) {
305290
for (const handler of [...this.#closeHandlers]) {
@@ -425,7 +410,9 @@ enc
425410
#handleOnOpen() {
426411
// Connection was disposed before Init message arrived - close the websocket to avoid leak
427412
if (this.#disposed) {
428-
logger().debug({ msg: "handleOnOpen called after dispose, closing websocket" });
413+
logger().debug({
414+
msg: "handleOnOpen called after dispose, closing websocket",
415+
});
429416
if (this.#websocket) {
430417
this.#websocket.close(1000, "Disposed");
431418
this.#websocket = undefined;
@@ -479,7 +466,7 @@ enc
479466

480467
const response = await this.#parseMessage(data as ConnMessage);
481468
logger().trace(
482-
getRivetkitLogMessage()
469+
getLogMessage()
483470
? {
484471
msg: "parsed message",
485472
message:
@@ -611,7 +598,10 @@ enc
611598

612599
// Automatically reconnect if we were connected
613600
if (wasConnected) {
614-
logger().debug({ msg: "triggering reconnect", connId: this.#connId });
601+
logger().debug({
602+
msg: "triggering reconnect",
603+
connId: this.#connId,
604+
});
615605
this.#connectWithRetry();
616606
}
617607
}
@@ -1097,7 +1087,10 @@ enc
10971087
// Close websocket (#handleOnClose will reject pending promises)
10981088
if (this.#websocket) {
10991089
const ws = this.#websocket;
1100-
if (ws.readyState !== 2 /* CLOSING */ && ws.readyState !== 3 /* CLOSED */) {
1090+
if (
1091+
ws.readyState !== 2 /* CLOSING */ &&
1092+
ws.readyState !== 3 /* CLOSED */
1093+
) {
11011094
const { promise, resolve } = promiseWithResolvers();
11021095
ws.addEventListener("close", () => resolve(undefined));
11031096
ws.close(1000, "Disposed");

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ export function getPinoLevel(logLevel?: LogLevel): LevelWithSilent {
4343
return configuredLogLevel;
4444
}
4545

46-
const raw = (getLogLevel() || "warn")
47-
.toString()
48-
.toLowerCase();
46+
const raw = (getLogLevel() || "warn").toString().toLowerCase();
4947

5048
const parsed = LogLevelSchema.safeParse(raw);
5149
if (parsed.success) {
@@ -129,10 +127,7 @@ export function configureDefaultLogger(logLevel?: LogLevel) {
129127
return { level: number };
130128
},
131129
},
132-
timestamp:
133-
getLogTimestamp()
134-
? stdTimeFunctions.epochTime
135-
: false,
130+
timestamp: getLogTimestamp() ? stdTimeFunctions.epochTime : false,
136131
browser: {
137132
write: {
138133
fatal: customWrite.bind(null, "fatal"),
@@ -156,10 +151,7 @@ export function configureDefaultLogger(logLevel?: LogLevel) {
156151
60: "fatal",
157152
};
158153
const levelName = levelMap[level] || "info";
159-
const time =
160-
getLogTimestamp()
161-
? Date.now()
162-
: undefined;
154+
const time = getLogTimestamp() ? Date.now() : undefined;
163155

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
} from "@/schemas/client-protocol-zod/mod";
2121
import { encodingIsBinary, serializeWithEncoding } from "@/serde";
2222
import { bufferToArrayBuffer, VERSION } from "@/utils";
23-
import { getRivetLogHeaders } from "@/utils/env-vars";
23+
import { getLogHeaders } from "@/utils/env-vars";
2424
import { getLogger, type Logger } from "./log";
2525
import { deconstructError, stringifyError } from "./utils";
2626

@@ -46,7 +46,7 @@ export function loggerMiddleware(logger: Logger) {
4646
reqSize: c.req.header("content-length"),
4747
resSize: c.res.headers.get("content-length"),
4848
userAgent: c.req.header("user-agent"),
49-
...(getRivetLogHeaders()
49+
...(getLogHeaders()
5050
? { allHeaders: JSON.stringify(c.req.header()) }
5151
: {}),
5252
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Next } from "hono";
22
import type { ContentfulStatusCode } from "hono/utils/http-status";
33
import * as errors from "@/actor/errors";
44
import { EXTRA_ERROR_LOG, VERSION } from "@/utils";
5-
import { getRivetkitErrorStack } from "@/utils/env-vars";
5+
import { getLogErrorStack } from "@/utils/env-vars";
66
import type { Logger } from "./log";
77

88
export function assertUnreachable(x: never): never {
@@ -301,7 +301,7 @@ export function stringifyError(error: unknown): string {
301301
if (error instanceof Error) {
302302
if (
303303
typeof process !== "undefined" &&
304-
getRivetkitErrorStack()
304+
getLogErrorStack()
305305
) {
306306
return `${error.name}: ${error.message}${error.stack ? `\n${error.stack}` : ""}`;
307307
} else {

rivetkit-typescript/packages/rivetkit/src/utils/env-vars.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ export const getRivetRunnerKind = (): string | undefined =>
2727
getEnvUniversal("RIVET_RUNNER_KIND");
2828

2929
// RivetKit configuration
30-
export const getRivetkitLogMessage = (): boolean =>
31-
!!getEnvUniversal("_RIVETKIT_LOG_MESSAGE");
3230
export const getRivetkitInspectorToken = (): string | undefined =>
3331
getEnvUniversal("RIVETKIT_INSPECTOR_TOKEN");
3432
export const getRivetkitInspectorDisable = (): boolean =>
35-
!!getEnvUniversal("RIVETKIT_INSPECTOR_DISABLE");
36-
export const getRivetkitErrorStack = (): boolean =>
37-
getEnvUniversal("_RIVETKIT_ERROR_STACK") === "1";
33+
getEnvUniversal("RIVETKIT_INSPECTOR_DISABLE") === "1";
3834

3935
// Logging configuration
4036
export const getLogLevel = (): string | undefined =>
41-
getEnvUniversal("LOG_LEVEL");
37+
getEnvUniversal("RIVETKIT_LOG_LEVEL");
4238
export const getLogTarget = (): boolean =>
43-
getEnvUniversal("LOG_TARGET") === "1";
39+
getEnvUniversal("RIVETKIT_LOG_TARGET") === "1";
4440
export const getLogTimestamp = (): boolean =>
45-
getEnvUniversal("LOG_TIMESTAMP") === "1";
46-
export const getRivetLogHeaders = (): boolean =>
47-
!!getEnvUniversal("_RIVET_LOG_HEADERS");
41+
getEnvUniversal("RIVETKIT_LOG_TIMESTAMP") === "1";
42+
export const getLogMessage = (): boolean =>
43+
getEnvUniversal("RIVETKIT_LOG_MESSAGE") === "1";
44+
export const getLogErrorStack = (): boolean =>
45+
getEnvUniversal("RIVETKIT_LOG_ERROR_STACK") === "1";
46+
export const getLogHeaders = (): boolean =>
47+
getEnvUniversal("RIVETKIT_LOG_HEADERS") === "1";
4848

4949
// Environment configuration
5050
export const getNodeEnv = (): string | undefined => getEnvUniversal("NODE_ENV");

vitest.base.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ export default {
1111
},
1212
env: {
1313
// Enable logging
14-
LOG_LEVEL: "DEBUG",
15-
LOG_TARGET: "1",
16-
LOG_TIMESTAMP: "1",
17-
_RIVETKIT_ERROR_STACK: "1",
18-
_RIVETKIT_LOG_MESSAGE: "1",
14+
RIVETKIT_LOG_LEVEL: "DEBUG",
15+
RIVETKIT_LOG_TARGET: "1",
16+
RIVETKIT_LOG_TIMESTAMP: "1",
17+
RIVETKIT_LOG_ERROR_STACK: "1",
18+
RIVETKIT_LOG_MESSAGE: "1",
1919
},
2020
},
2121
} satisfies ViteUserConfig;

website/src/content/docs/general/architecture.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ actor definitions include the following generic parameters that you'll see frequ
153153
- rivet uses pino for logging
154154
- we expose a scoped child logger for each actor at `c.log` that automatically logs the actor id + key
155155
- this allows you to search lgos easily by actor id without having to log the actor id frequently
156-
- logs can be configured via the `LOG_LEVEL` env var
156+
- logs can be configured via the `RIVETKIT_LOG_LEVEL` env var
157157

158158
### fault tolerance
159159

website/src/content/docs/general/logging.mdx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,16 @@ You can configure logging behavior using environment variables:
5454

5555
| Variable | Description | Values | Default |
5656
| -------- | ----------- | ------ | ------- |
57-
| `LOG_LEVEL` | Sets the minimum log level to display | `trace`, `debug`, `info`, `warn`, `error`, `fatal`, `silent` | `warn` |
58-
| `LOG_TARGET` | Include the module name that logged the message | `1` to enable, `0` to disable | `0` |
59-
| `LOG_TIMESTAMP` | Include timestamp in log output | `1` to enable, `0` to disable | `0` |
57+
| `RIVETKIT_LOG_LEVEL` | Sets the minimum log level to display | `trace`, `debug`, `info`, `warn`, `error`, `fatal`, `silent` | `warn` |
58+
| `RIVETKIT_LOG_TARGET` | Include the module name that logged the message | `1` to enable, `0` to disable | `0` |
59+
| `RIVETKIT_LOG_TIMESTAMP` | Include timestamp in log output | `1` to enable, `0` to disable | `0` |
60+
| `RIVETKIT_LOG_MESSAGE` | Enable detailed message logging for debugging | `1` to enable, `0` to disable | `0` |
61+
| `RIVETKIT_LOG_ERROR_STACK` | Include stack traces in error output | `1` to enable, `0` to disable | `0` |
62+
| `RIVETKIT_LOG_HEADERS` | Log HTTP headers in requests | `1` to enable, `0` to disable | `0` |
6063

6164
Example:
6265
```bash
63-
LOG_LEVEL=debug LOG_TARGET=1 LOG_TIMESTAMP=1 node server.js
66+
RIVETKIT_LOG_LEVEL=debug RIVETKIT_LOG_TARGET=1 RIVETKIT_LOG_TIMESTAMP=1 node server.js
6467
```
6568

6669
### Log Level

0 commit comments

Comments
 (0)