Sanity Library Reference Docs
    Preparing search index...

    Function configureLogging

    • Configure logging for the Sanity SDK

      This function allows you to control what logs are output by the SDK, making it easier to debug issues in development or production.

      Parameters

      Returns void

      Zero-Config via Environment Variable (Recommended):

      The SDK automatically reads the DEBUG environment variable, making it easy to enable logging without code changes:

      # Enable all SDK logging at debug level
      DEBUG=sanity:* npm start

      # Enable specific namespaces
      DEBUG=sanity:auth,sanity:document npm start

      # Enable trace level for all namespaces
      DEBUG=sanity:trace:* npm start

      # Enable internal/maintainer logs
      DEBUG=sanity:*:internal npm start

      This matches the pattern used by Sanity CLI and Studio, making it familiar and easy for support teams to help troubleshoot issues.

      Programmatic Configuration (Advanced):

      For more control (custom handlers, dynamic configuration), call this function explicitly. Programmatic configuration overrides environment variables.

      For Application Developers: Use info, warn, or error levels to see high-level SDK activity without being overwhelmed by internal details.

      For SDK Maintainers: Use debug or trace levels with internal: true to see detailed information about store operations, RxJS streams, and state transitions.

      Instance Context: Logs automatically include instance information (projectId, dataset, instanceId) when available, making it easier to debug multi-instance scenarios:

      [INFO] [auth] [project:abc] [dataset:production] User logged in
      

      Available Namespaces:

      • sdk - SDK initialization, configuration, and lifecycle
      • auth - Authentication and authorization (when instrumented in the future)
      • And more as logging is added to modules
      # Just set DEBUG and run your app - no code changes needed!
      DEBUG=sanity:* npm start
      import {configureLogging} from '@sanity/sdk'

      // Log warnings and errors for auth and document operations
      configureLogging({
      level: 'warn',
      namespaces: ['auth', 'document']
      })
      import {configureLogging} from '@sanity/sdk'

      // Enable all logs including internal traces
      configureLogging({
      level: 'trace',
      namespaces: ['*'],
      internal: true
      })
      import {configureLogging} from '@sanity/sdk'

      const logs: string[] = []
      configureLogging({
      level: 'info',
      namespaces: ['*'],
      handler: {
      error: (msg) => logs.push(msg),
      warn: (msg) => logs.push(msg),
      info: (msg) => logs.push(msg),
      debug: (msg) => logs.push(msg),
      trace: (msg) => logs.push(msg),
      }
      })