Skip to content

RFC: allow users to pass a custom logger to init#2745

Merged
dwwoelfel merged 5 commits into
mainfrom
custom-logger
Jun 8, 2026
Merged

RFC: allow users to pass a custom logger to init#2745
dwwoelfel merged 5 commits into
mainfrom
custom-logger

Conversation

@dwwoelfel

Copy link
Copy Markdown
Contributor

This is an attempt to address #2742 without making the logs harder to read in the devtools console.

Users can pass a custom logger where they format things however they like. If you wanted to JSON stringify, you might do:

const db = init({
  ...config,
  schema,
  verbose: true,
  logger: {
    debug: (...args) => console.debug(JSON.stringify(args)),
    info: (...args) => console.info(JSON.stringify(args)),
    error: (...args) => console.error(JSON.stringify(args)),
  },
});

Or if you just wanted to json.stringify objects, you could do:

const db = init({
  ...config,
  schema,
  verbose: true,
  logger: {
    debug: (...args) =>
      console.debug(
        ...args.map((a) => (typeof a === 'object' ? JSON.stringify(a) : a)),
      ),
    info: (...args) =>
      console.info(
        ...args.map((a) => (typeof a === 'object' ? JSON.stringify(a) : a)),
      ),
    error: (...args) =>
      console.error(
        ...args.map((a) => (typeof a === 'object' ? JSON.stringify(a) : a)),
      ),
  },
});

@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: fe5532d4-0389-48c6-a5d3-009cd7583a15

📥 Commits

Reviewing files that changed from the base of the PR and between 0afe605 and 66be1fb.

📒 Files selected for processing (1)
  • client/packages/version/src/version.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • client/packages/version/src/version.ts

📝 Walkthrough

Walkthrough

Adds an injectable Logger across client packages: core logging accepts a baseLogger, Config/InstantConfig types gain an optional logger field, Reactor and admin database wire the injected logger, framework packages re-export the Logger type, and the package version is bumped.

Changes

Custom Logger Injection

Layer / File(s) Summary
Core logger function enhancement
client/packages/core/src/utils/log.ts
createLogger now accepts an optional baseLogger parameter (defaulting to console) and routes info, debug, and error calls to that logger instead of hard-coded console references.
Core configuration types
client/packages/core/src/index.ts
Config and InstantConfig types are extended with optional logger?: Logger field, and Logger type is imported and exported from core's public API.
Reactor logger integration
client/packages/core/src/Reactor.js
Reactor constructor passes config.logger to createLogger, enabling the injected logger in core reactor initialization.
Admin package implementation
client/packages/admin/src/index.ts
Admin package imports Logger, adds optional logger to Config/InstantConfig, replaces the local logger factory with the core-compatible createLogger(isEnabled, baseLogger), wires config.logger into InstantAdminDatabase, and re-exports Logger.
Framework package type exports
client/packages/react/src/index.ts, client/packages/react-native/src/index.ts, client/packages/solidjs/src/index.ts, client/packages/svelte/src/lib/index.ts, client/packages/vue/src/index.ts
Each framework package imports and re-exports the Logger type from core to expose it in their public type surfaces.
Version bump
client/packages/version/src/version.ts
Package version updated from v1.0.44 to v1.0.45.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • instantdb/instant#2537: Overlaps changes to admin config/logging initialization surface where verbose and logger wiring interact.

Suggested reviewers

  • stopachka
  • nezaj
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: allowing users to pass a custom logger to the init function, which aligns with all modifications across the codebase.
Description check ✅ Passed The description clearly explains the feature's purpose, provides concrete usage examples, and addresses the referenced issue, directly relating to the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch custom-logger

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

View Vercel preview at instant-www-js-custom-logger-jsv.vercel.app.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
client/packages/admin/src/index.ts (1)

1107-1116: ⚡ Quick win

Consider importing createLogger from core to reduce duplication.

The admin package defines its own createLogger helper, but the core package already exports a similar function (see client/packages/core/src/utils/log.ts). The core version includes a getStats parameter for appending diagnostic info to logs, which the admin version omits.

If the admin package doesn't need stats, you could:

  1. Import the core createLogger and pass a no-op getStats: () => ({}), or
  2. Propose making getStats optional in the core version with a default no-op

This would eliminate duplication and ensure consistent logger behavior across packages.

♻️ Example: Import from core
+import createLogger from '`@instantdb/core/src/utils/log`';
+
 class InstantAdminDatabase<...> {
   ...
   constructor(_config: Config) {
     ...
-    this.#log = createLogger(!!this.config.verbose, this.config.logger);
+    this.#log = createLogger(
+      !!this.config.verbose,
+      () => ({}),  // no-op getStats
+      this.config.logger,
+    );
   }
 }
-
-function createLogger(
-  isEnabled: boolean,
-  baseLogger: Logger = console,
-): Logger {
-  return {
-    info: isEnabled ? (...args: any[]) => baseLogger.info(...args) : () => {},
-    debug: isEnabled ? (...args: any[]) => baseLogger.debug(...args) : () => {},
-    error: isEnabled ? (...args: any[]) => baseLogger.error(...args) : () => {},
-  };
-}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@client/packages/admin/src/index.ts` around lines 1107 - 1116, The admin
package redefines createLogger, duplicating logic from core's createLogger (see
client/packages/core/src/utils/log.ts) which accepts an additional getStats
parameter; fix by removing the local createLogger and importing the core
createLogger into client/packages/admin/src/index.ts, calling it with the same
isEnabled and baseLogger arguments and supplying a no-op getStats (() => ({}))
if admin doesn't need stats — alternatively, change the core createLogger
signature to make getStats optional with a default no-op so admin can import and
call it unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@client/packages/admin/src/index.ts`:
- Around line 1107-1116: The admin package redefines createLogger, duplicating
logic from core's createLogger (see client/packages/core/src/utils/log.ts) which
accepts an additional getStats parameter; fix by removing the local createLogger
and importing the core createLogger into client/packages/admin/src/index.ts,
calling it with the same isEnabled and baseLogger arguments and supplying a
no-op getStats (() => ({})) if admin doesn't need stats — alternatively, change
the core createLogger signature to make getStats optional with a default no-op
so admin can import and call it unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 0ce2225d-e475-4fa7-ab8d-70c53f6fc755

📥 Commits

Reviewing files that changed from the base of the PR and between 6d97f6d and 50f9652.

📒 Files selected for processing (9)
  • client/packages/admin/src/index.ts
  • client/packages/core/src/Reactor.js
  • client/packages/core/src/index.ts
  • client/packages/core/src/utils/log.ts
  • client/packages/react-native/src/index.ts
  • client/packages/react/src/index.ts
  • client/packages/solidjs/src/index.ts
  • client/packages/svelte/src/lib/index.ts
  • client/packages/vue/src/index.ts

@nezaj nezaj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@dwwoelfel dwwoelfel merged commit 50a2c63 into main Jun 8, 2026
23 checks passed
@dwwoelfel dwwoelfel deleted the custom-logger branch June 8, 2026 20:36
@nezaj nezaj mentioned this pull request Jun 9, 2026
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.

2 participants