Skip to main content
Reference engineering Archived 35 min read Updated

docs-how-to-logging

\ Logging | SpacetimeDB docs \ \document.documentElement.setAttribute("data-theme","light"),document.documentElement.setAttribute("data-theme-choice","light"),function(){try{const c=new URLSearchParams(window.location.search).entries();for(var[t,e]of c)if(t.startsWith("docusaurus-data-")){var a=t.replace("docusaurus-data-","data-");document.documentElement.setAttribute(a,e)}}catch(t){}}()
\Version: 2.0.0
\\Logging

SpacetimeDB provides logging capabilities for debugging and monitoring your modules. Log messages are private to the database owner and are not visible to clients.

Writing Logs​

Use the standard console API to write logs from your reducers:

import { schema, t } from 'spacetimedb/server';

const spacetimedb = schema({ /* tables */ });
export default spacetimedb;

export const processData = spacetimedb.reducer({ value: t.u32() }, (ctx, { value }) => {
  console.log(`Processing data with value: ${value}`);
  
  if (value > 100) {
    console.warn(`Value ${value} exceeds threshold`);
  }
  
  if (value === 0) {
    console.error('Invalid value: 0');
    throw new Error('Value cannot be zero');
  }
  
  console.debug(`Debug information: ctx.sender = ${ctx.sender}`);
});

Available console methods:

\
  • console.error() - Error messages
  • console.warn() - Warning messages
  • console.log() - Informational messages
  • console.debug() - Debug messages
  • SpacetimeDB automatically routes these standard console calls through its internal logging system.

    Viewing Logs​

    To view logs from your database, use the spacetime logs command:

    spacetime logs <DATABASE_NAME>

    Following Logs in Real-Time​

    To stream logs as they're generated (similar to tail -f):

    spacetime logs --follow <DATABASE_NAME>

    Filtering Logs​

    You can filter logs by various criteria:

    # Show only errors
    spacetime logs --level error <DATABASE_NAME>
    
    # Show warnings and errors
    spacetime logs --level warn <DATABASE_NAME>
    
    # Show logs from a specific time range
    spacetime logs --since "2023-01-01 00:00:00" <DATABASE_NAME>

    For all log viewing options, see the spacetime logs CLI reference.

    Best Practices​

    Log Levels​

    Use appropriate log levels for different types of messages:

    \
  • Error: Use for actual errors that prevent operations from completing
  • Warn: Use for potentially problematic situations that don't prevent execution
  • Info: Use for important application events (user actions, state changes)
  • Debug: Use for detailed diagnostic information useful during development
  • Trace: Use for very detailed diagnostic information (typically disabled in production)
  • Performance Considerations​

    \
  • Logging has minimal overhead, but excessive logging can impact performance
  • Avoid logging in tight loops or high-frequency operations
  • Consider using debug/trace logs for verbose output that can be filtered in production
  • Privacy and Security​

    \
  • Logs are only visible to the database owner, not to clients
  • Avoid logging sensitive information like passwords or authentication tokens
  • Be mindful of personally identifiable information (PII) in logs
  • Structured Logging​

    Include relevant context in your log messages:

    export const transferCredits = spacetimedb.reducer(
      { toUser: t.u64(), amount: t.u32() },
      (ctx, { toUser, amount }) => {
        console.log(`Credit transfer: from=${ctx.sender}, to=${toUser}, amount=${amount}`);
        
        // ... transfer logic
      }
    );

    Next Steps​

    \
  • Learn about Error Handling in reducers
  • Explore the CLI Reference for more logging options
  • Set up monitoring and alerting for your production databases