Skip to content

Allow customizing logger for SQL errors #767

@dknecht

Description

@dknecht

Problem

The sql method in Agent uses console.error directly for logging SQL errors:

} catch (e) {
  console.error(`failed to execute sql query: ${query}`, e);
  throw this.onError(e);
}

This bypasses any structured logging the application may have configured. During DO resets (e.g., code deployments), this produces unstructured logs like:

(error) failed to execute sql query:
    INSERT OR REPLACE INTO cf_agents_state (id, state)
    VALUES (?, ?)
   Error: Durable Object reset because its code was updated.

Proposal

Allow users to customize logging behavior, either via:

  1. Override method - onSqlError(query: string, error: unknown) that defaults to console.error but can be overridden
  2. Logger injection - Accept a logger in Agent options/constructor
  3. Use observability - Route through the existing observability interface

Option 1 is minimal and consistent with existing patterns like onError.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions