Skip to content

TypeScript SDK: allow replacing the logger with setGlobalLogger - #6149

Open
mausworks wants to merge 1 commit into
clockworklabs:masterfrom
mausworks:ts-sdk/custom-logger
Open

mausworks wants to merge 1 commit into
clockworklabs:masterfrom
mausworks:ts-sdk/custom-logger

Conversation

@mausworks

Copy link
Copy Markdown

Description of Changes

The TypeScript SDK's log output always goes to console.log with %c styling. Only the level can be configured (setGlobalLogLevel), so there's no way to:

  • forward SDK logs to an app's own logger or error reporting, or
  • get readable output where %c styling isn't supported. In React Native, for example, every line shows the raw %c markers and CSS strings.

This adds a pluggable sink next to the existing level setting:

import { setGlobalLogger, consoleLogger, type Logger } from 'spacetimedb';

setGlobalLogger((level, message, ...args) => {
  console[level === 'trace' ? 'debug' : level](`[stdb] ${message}`, ...args);
});

setGlobalLogger(consoleLogger); // back to the default
  • Logger: (level, message, ...args) => void. It's only called for logs that pass the global log level, with lazy messages and arguments already resolved.
  • setGlobalLogger and getGlobalLogger: replace or read the current sink, mirroring setGlobalLogLevel and getGlobalLogLevel.
  • consoleLogger: the existing styled console.log output, unchanged and still the default.

API and ABI breaking changes

None. This only adds exports, and the default output is unchanged.

Rollback safety impact

n/a

Expected complexity level and risk

  1. It's a small, client-only change in src/sdk/logger.ts.

Testing

  • Added tests/logger.test.ts cases:
    • a custom logger receives resolved logs instead of the console;
    • it still respects the global log level, and lazy messages aren't evaluated when filtered out;
    • consoleLogger restores the default output.
  • vitest run: all 417 tests pass. Locally, server_test_utils_wasm*.test.ts can't load because the portable datastore WASM build is missing; that's unrelated to this change.
  • ESLint didn't run locally because the root dependencies weren't installed, so it relies on CI.

🤖 Generated with Claude Code

@CLAassistant

CLAassistant commented Oct 12, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@mausworks

Copy link
Copy Markdown
Author

Confirmed working great in my expo project with a plain logger:

setGlobalLogger((level, message, ...args) => {
  console[level](message, ...args);
});

The SDK's log output always goes to `console.log` with `%c` styling, which
can't be redirected to an app's own logger, and shows up as raw `%c` and CSS
strings where styling isn't supported (e.g. React Native). Adds
`setGlobalLogger`/`getGlobalLogger`, which receive resolved logs after the
global log level is applied, and exports the existing output as
`consoleLogger` (still the default).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mausworks
mausworks force-pushed the ts-sdk/custom-logger branch from 65485e0 to 82ca59e Compare October 14, 2026 14:25

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants