Log Monitoring

Production-grade log monitoring built into a11ops. Capture, group, and analyze application logs and errors with full context. Automatically detect anomalies and get alerted on critical issues.

Understanding Log Monitoring

Log monitoring in a11ops captures and analyzes application logs and errors, providing:

  • Automatic Error Capture - Catch unhandled exceptions and errors
  • Smart Grouping - Similar errors are grouped by stack trace fingerprinting
  • Context Collection - User data, breadcrumbs, and environment info
  • Spike Detection - Automatic alerts when error rates spike
  • Release Tracking - Track errors by version and deployment

Real-time Analysis

Logs are processed in real-time with automatic grouping, deduplication, and anomaly detection.

Intelligent Filtering

Filter by severity, environment, release, user, or any custom tags to find issues quickly.

Quick Start

1. Initialize with log monitoring enabled

import A11ops from '@a11ops/sdk';

const client = new A11ops('your-api-key', {
  logMonitoring: true,
  environment: 'production',
  release: '1.0.0'
});

// Automatic log and error capture is now enabled

Log Event Structure

Example Log Entry

{
  "message": "Database connection timeout",
  "type": "DatabaseError",
  "level": "error",
  "stack_trace": "Error: Connection timeout
  at connectDB (db.js:45)
  at async handler (api.js:12)",
  "timestamp": "2024-01-15T10:30:45Z",
  "environment": "production",
  "release": "2.1.0",
  "user": {
    "id": "user_123",
    "email": "user@example.com"
  },
  "tags": {
    "database": "postgres",
    "region": "us-east-1"
  },
  "breadcrumbs": [
    {
      "type": "http",
      "message": "GET /api/users",
      "timestamp": "2024-01-15T10:30:40Z"
    }
  ]
}

Full Context

Capture complete context including user data, breadcrumbs, and environment information with every log entry.

Alert Rules

Configure rules to automatically convert critical logs into push notifications based on patterns and thresholds.

Manual Error Capture

Capture specific errors with context

try {
  await processPayment(order);
} catch (error) {
  client.logs.captureError(error, {
    level: 'error',
    tags: { 
      module: 'payment',
      payment_method: order.paymentMethod 
    },
    user: {
      id: user.id,
      email: user.email
    },
    extra: {
      order_id: order.id,
      amount: order.total
    }
  });
}

Breadcrumbs & Context

Track user actions and system events leading up to errors for better debugging.

Add breadcrumbs

// Track user actions
client.logs.addBreadcrumb({
  message: 'User clicked checkout',
  category: 'user-action',
  level: 'info'
});

// Track API calls
client.logs.addBreadcrumb({
  message: 'Payment API call',
  category: 'api',
  data: { endpoint: '/api/payment', status: 200 }
});

Set user context

// Identify the user
client.logs.setUser({
  id: 'user-123',
  email: 'user@example.com',
  username: 'johndoe',
  subscription: 'premium'
});

Set tags and context

// Set tags for filtering
client.logs.setTag('feature', 'checkout');
client.logs.setTag('browser', 'chrome');

// Set additional context
client.logs.setContext('session', {
  id: sessionId,
  started: new Date(),
  page_views: 5
});

Framework Integration

Express.js

// Error middleware
app.use((err, req, res, next) => {
  client.logs.captureError(err, {
    user: req.user,
    extra: {
      method: req.method,
      url: req.url,
      headers: req.headers
    }
  });
  
  res.status(500).json({ error: 'Internal Server Error' });
});

Next.js

// pages/_error.js or app/error.tsx
export default function Error({ error }) {
  useEffect(() => {
    client.logs.captureError(error);
  }, [error]);
  
  return <ErrorPage />;
}

React Error Boundary

class ErrorBoundary extends React.Component {
  componentDidCatch(error, errorInfo) {
    client.logs.captureError(error, {
      extra: errorInfo
    });
  }
  
  render() {
    if (this.state.hasError) {
      return <h1>Something went wrong.</h1>;
    }
    return this.props.children;
  }
}

Error Levels

LevelUsage
debugDiagnostic information
infoInformational messages
warningWarning conditions
errorError conditions
fatalCritical failures
criticalSystem-critical issues

Alert Rules

Configure automatic alerts when errors meet certain conditions.

Example rule configuration

{
  "name": "Critical Error Alert",
  "conditions": {
    "error_level": "critical",
    "min_occurrences": 1,
    "time_window": "5m"
  },
  "alert_config": {
    "severity": "critical",
    "title_template": "Critical Error: {error.message}",
    "message_template": "{error.type} occurred {error.occurrence_count} times"
  }
}

Best Practices

Set user context early

Call setUser() as soon as the user is identified to associate all errors with the user.

Use meaningful tags

Tag errors with feature names, modules, and environments for better filtering.

Add breadcrumbs for critical paths

Track important user actions and API calls to understand error context.

Use appropriate error levels

Reserve 'critical' and 'fatal' for issues requiring immediate attention.

Next Steps

Ready to start monitoring your application logs and errors?