# Railway CLI Command Library

Comprehensive reference for Railway CLI commands optimized for debugging and monitoring.

## 📋 Table of Contents

1. [Deployment Management](#deployment-management)
2. [Log Viewing](#log-viewing)
3. [Filtering & Searching](#filtering--searching)
4. [Time-Based Filtering](#time-based-filtering)
5. [Granularity Levels](#granularity-levels)
6. [Status & Monitoring](#status--monitoring)
7. [Best Practices](#best-practices)

---

## Deployment Management

### List Deployments

```bash
# List recent deployments with metadata
railway deployment list --service slide-system --limit 20

# Get JSON output for parsing
railway deployment list --service slide-system --limit 10 --json

# Parse deployment IDs and status
railway deployment list --service slide-system --json | \
  jq -r '.[] | "\(.id) | \(.status) | \(.createdAt)"'

# Get commit hashes from deployments
railway deployment list --service slide-system --json | \
  jq -r '.[] | "\(.meta.commitHash // "N/A") | \(.status)"'
```

### Check Current Status

```bash
# Get project status
railway status

# Get JSON output
railway status --json
```

---

## Log Viewing

### Basic Log Commands

```bash
# Stream latest deployment logs (default behavior)
railway logs

# Get last N lines without streaming
railway logs --lines 100
railway logs -n 50  # Short alias

# Get build logs only
railway logs --build

# Get deployment/runtime logs only
railway logs --deployment

# Get logs in JSON format
railway logs --json --lines 50
```

### Service-Specific Logs

```bash
# Logs from specific service
railway logs --service slide-system

# Logs from specific service and environment
railway logs --service slide-system --environment production

# Logs from specific deployment ID
railway logs 1b71c5b3-2908-49a4-9413-c43bcb753952

# Build logs from specific deployment
railway logs --build 1b71c5b3-2908-49a4-9413-c43bcb753952
```

---

## Filtering & Searching

### Text Search

```bash
# Search for keyword
railway logs --filter "asyncio" --lines 50

# Search for exact phrase
railway logs --filter "\"POST /api\"" --lines 100

# Multiple keywords
railway logs --filter "error connection failed" --lines 50
```

### Attribute Filtering

```bash
# Filter by log level
railway logs --filter "@level:error" --lines 50
railway logs --filter "@level:warn" --lines 50
railway logs --filter "@level:info" --lines 100

# Combine level with keywords
railway logs --filter "@level:error AND database" --lines 50
railway logs --filter "@level:warn AND rate limit" --lines 30
```

### HTTP Log Filtering

```bash
# Filter by HTTP method
railway logs --filter "@method:POST" --lines 50

# Filter by HTTP status code
railway logs --filter "@httpStatus:500" --lines 50
railway logs --filter "@httpStatus:200" --lines 100

# Filter by URL path
railway logs --filter "@path:/api/sessions/voice-demo" --lines 50

# Combine HTTP filters
railway logs --filter "@path:/api AND @httpStatus:500" --lines 50
```

### Advanced Filtering

```bash
# Exclude specific logs
railway logs --filter "-@httpStatus:200" --lines 50

# OR operator
railway logs --filter "@level:error OR @level:warn" --lines 50

# Complex combinations
railway logs --filter "@level:error AND database AND -timeout" --lines 50
```

---

## Time-Based Filtering

Railway CLI doesn't have built-in time-based filtering, but you can use `--json` output with `jq` for powerful time-based queries.

### JSON Structure

Each log line in JSON format contains:
```json
{
  "message": "Log message text",
  "timestamp": "2025-10-18T07:48:52.697855561Z",
  "level": "info"
}
```

### Time-Based Filtering Examples

```bash
# Logs from last 5 minutes (macOS)
railway logs --service slide-system --lines 1000 --json | \
  jq -r --arg since "$(date -u -v-5M +%Y-%m-%dT%H:%M:%SZ)" \
  'select(.timestamp >= $since) | "\(.timestamp) | \(.level) | \(.message)"'

# Logs from last 5 minutes (Linux)
railway logs --service slide-system --lines 1000 --json | \
  jq -r --arg since "$(date -u -d '5 minutes ago' +%Y-%m-%dT%H:%M:%SZ)" \
  'select(.timestamp >= $since) | "\(.timestamp) | \(.level) | \(.message)"'

# Logs from last hour
railway logs --service slide-system --lines 2000 --json | \
  jq -r --arg since "$(date -u -v-1H +%Y-%m-%dT%H:%M:%SZ)" \
  'select(.timestamp >= $since) | .message'

# Logs between two specific times
railway logs --service slide-system --lines 2000 --json | \
  jq -r 'select(.timestamp >= "2025-10-18T07:45:00Z" and .timestamp <= "2025-10-18T07:50:00Z") | .message'

# Count logs in time window
railway logs --service slide-system --lines 1000 --json | \
  jq -r --arg since "$(date -u -v-10M +%Y-%m-%dT%H:%M:%SZ)" \
  'select(.timestamp >= $since)' | wc -l

# Errors in last 30 minutes
railway logs --service slide-system --lines 1000 --json | \
  jq -r --arg since "$(date -u -v-30M +%Y-%m-%dT%H:%M:%SZ)" \
  'select(.timestamp >= $since and .level == "error") | "\(.timestamp) | \(.message)"'

# Bot spawns in last 15 minutes
railway logs --service slide-system --lines 500 --json | \
  jq -r --arg since "$(date -u -v-15M +%Y-%m-%dT%H:%M:%SZ)" \
  'select(.timestamp >= $since and (.message | contains("spawned successfully"))) | .message'
```

### Time Window Analysis

```bash
# Get all bot events in a specific time window
railway logs --service slide-system --lines 2000 --json | \
  jq -r 'select(.timestamp >= "2025-10-18T07:45:00Z" and .timestamp <= "2025-10-18T08:00:00Z" and (.message | contains("Bot"))) | "\(.timestamp) | \(.message)"'

# Timeline of deployment events
railway logs --service slide-system --lines 1000 --json | \
  jq -r 'select(.message | test("Starting|Stopping|Build succeeded")) | "\(.timestamp) | \(.message)"'

# Hourly error count
railway logs --service slide-system --lines 5000 --json | \
  jq -r 'select(.level == "error") | .timestamp[:13]' | uniq -c
```

### Time-Based Performance Queries

```bash
# Average errors per minute in last hour
railway logs --service slide-system --lines 3000 --json | \
  jq -r --arg since "$(date -u -v-1H +%Y-%m-%dT%H:%M:%SZ)" \
  'select(.timestamp >= $since and .level == "error")' | wc -l | \
  awk '{print $1 / 60 " errors/min"}'

# Bot spawn success rate in last 30 minutes
SUCCESS=$(railway logs --service slide-system --lines 500 --json | \
  jq -r --arg since "$(date -u -v-30M +%Y-%m-%dT%H:%M:%SZ)" \
  'select(.timestamp >= $since and (.message | contains("spawned successfully")))' | wc -l)
FAILED=$(railway logs --service slide-system --lines 500 --json | \
  jq -r --arg since "$(date -u -v-30M +%Y-%m-%dT%H:%M:%SZ)" \
  'select(.timestamp >= $since and (.message | contains("spawn.failed")))' | wc -l)
echo "Success: $SUCCESS, Failed: $FAILED"
```

---

## Granularity Levels

Control log filtering from very broad (all logs) to very fine-grained (specific bot PIDs, specific errors).

### Level 1: BROAD - Get Everything

```bash
# All logs from current deployment (streaming)
railway logs --service slide-system

# All logs from last 500 lines
railway logs --service slide-system --lines 500

# All logs in JSON format for processing
railway logs --service slide-system --lines 1000 --json

# All build logs
railway logs --service slide-system --build --lines 200
```

**Use When:** Initial investigation, need full context, exploring unknown issues

### Level 2: CATEGORY - Filter by Type

```bash
# All errors (any source)
railway logs --service slide-system --filter "@level:error" --lines 100

# All warnings
railway logs --service slide-system --filter "@level:warn" --lines 100

# All bot-related logs (any bot)
railway logs --service slide-system --filter "Bot" --lines 200

# All deployment events
railway logs --service slide-system --filter "Starting Container OR Stopping Container" --lines 50

# All API requests
railway logs --service slide-system --filter "@path:/api" --lines 100
```

**Use When:** Know general category, want to see patterns across all instances

### Level 3: MEDIUM - Combine Categories

```bash
# Bot errors only
railway logs --service slide-system --filter "@level:error AND Bot" --lines 50

# Specific endpoint errors
railway logs --service slide-system --filter "@path:/api/sessions/voice-demo AND @httpStatus:500" --lines 50

# Bot spawn events (success + failure)
railway logs --service slide-system --filter "spawned successfully OR spawn.failed" --lines 50

# Warnings related to bot lifecycle
railway logs --service slide-system --filter "@level:warn AND (Bot OR spawn)" --lines 50

# HTTP 4xx and 5xx errors
railway logs --service slide-system --filter "@httpStatus:4* OR @httpStatus:5*" --lines 100
```

**Use When:** Narrowing down to specific category interactions, investigating related issues

### Level 4: FINE-GRAINED - Specific Components

```bash
# Specific bot ID logs
railway logs --service slide-system --filter "Bot 563aa1cd" --lines 100

# Multiple specific bots
railway logs --service slide-system --filter "Bot 563aa1cd OR Bot b70bfc6a" --lines 100

# Specific import errors
railway logs --service slide-system --filter "ModuleNotFoundError" --lines 50

# Specific step errors
railway logs --service slide-system --filter "ScreenshareBotSpawnLiveKit" --lines 50

# Python-specific errors
railway logs --service slide-system --filter "Traceback OR ModuleNotFoundError OR ImportError" --lines 100
```

**Use When:** Debugging specific component, tracking specific bot, investigating known error type

### Level 5: VERY FINE - Precise Queries

```bash
# Specific bot's STDOUT only
railway logs --service slide-system --filter "Bot 563aa1cd STDOUT" --lines 100

# Specific bot's STDERR only
railway logs --service slide-system --filter "Bot 563aa1cd STDERR" --lines 50

# Exact error message
railway logs --service slide-system --filter "\"No module named 'PIL'\"" --lines 20

# Specific function calls
railway logs --service slide-system --filter "asyncio_spec.origin" --lines 20

# Specific deployment events for one bot
railway logs --service slide-system --filter "Bot 563aa1cd AND (spawned OR stopped OR error)" --lines 50

# Import statements only
railway logs --service slide-system --filter "Importing OR imported successfully" --lines 50
```

**Use When:** Deep debugging, need exact log lines, tracing specific execution path

### Granularity Comparison

```bash
# Example: Finding bot import issues

# BROAD: All bot logs (1000s of lines)
railway logs --service slide-system --filter "Bot" --lines 500

# MEDIUM: Bot errors only (100s of lines)
railway logs --service slide-system --filter "@level:error AND Bot" --lines 100

# FINE: Bot import errors (10s of lines)
railway logs --service slide-system --filter "Bot AND (ModuleNotFoundError OR ImportError)" --lines 50

# VERY FINE: Specific bot's PIL import error (1-5 lines)
railway logs --service slide-system --filter "Bot 563aa1cd AND \"No module named 'PIL'\"" --lines 10
```

### Dynamic Granularity Workflow

```bash
# 1. Start broad - what's happening?
railway logs --service slide-system --lines 100

# 2. Narrow to category - any errors?
railway logs --service slide-system --filter "@level:error" --lines 50

# 3. Medium specificity - bot-related errors?
railway logs --service slide-system --filter "@level:error AND Bot" --lines 50

# 4. Fine-grained - which bot IDs?
railway logs --service slide-system --filter "@level:error AND Bot" --lines 50 | grep -oE "Bot [a-f0-9-]+" | sort -u

# 5. Very fine - specific bot's error
railway logs --service slide-system --filter "Bot 563aa1cd STDERR" --lines 100
```

---

## Status & Monitoring

### Bot-Specific Monitoring

```bash
# Monitor bot spawn events
railway logs --filter "Bot.*STDOUT" --lines 100
railway logs --filter "Spawning bot" --lines 50

# Check bot errors
railway logs --filter "Bot.*STDERR" --lines 100

# Track specific bot by ID
railway logs --filter "Bot 563aa1cd" --lines 50

# Monitor asyncio-related logs
railway logs --filter "asyncio" --lines 50
```

### Deployment Tracking

```bash
# Check if new deployment started
railway logs --filter "Starting Container" --lines 10

# Check deployment commit
railway logs --filter "commit" --lines 10

# Monitor build status
railway logs --build --filter "Build succeeded" --lines 10
```

### Real-Time Monitoring

```bash
# Stream logs with grep filter (external)
railway logs | grep -E "(error|warn|fail)"

# Stream specific bot logs
railway logs | grep "Bot.*STDOUT"

# Monitor deployment changes
railway logs | grep -E "(Starting|Stopping) Container"
```

---

## Best Practices

### 1. Use JSON for Automation

```bash
# Parse structured data
railway deployment list --json | jq '.[] | select(.status == "SUCCESS")'

# Extract specific fields
railway logs --json --lines 50 | jq -r '.[] | select(.level == "error") | .message'
```

### 2. Combine CLI with Unix Tools

```bash
# Get logs and count errors
railway logs --lines 500 | grep -c "error"

# Extract bot IDs
railway logs --lines 200 | grep "Bot.*STDOUT" | sed 's/.*Bot \([a-f0-9-]*\).*/\1/' | sort -u

# Timeline of events
railway logs --lines 100 | grep -E "(Starting|Stopping|error)" | head -20
```

### 3. Deployment Investigation Workflow

```bash
# 1. List recent deployments
railway deployment list --limit 5

# 2. Get logs from specific deployment
railway logs <deployment-id> --lines 200

# 3. Filter for errors
railway logs <deployment-id> --filter "@level:error" --lines 50

# 4. Check build logs if runtime fails
railway logs --build <deployment-id> --lines 100
```

### 4. Bot Debugging Workflow

```bash
# 1. Find recent bot spawns
railway logs --filter "spawned successfully" --lines 20

# 2. Get bot ID and check its logs
railway logs --filter "Bot <bot-id>" --lines 100

# 3. Check for import errors
railway logs --filter "Bot <bot-id>.*STDERR" --lines 50

# 4. Verify environment
railway logs --filter "Bot <bot-id>.*Python executable" --lines 10
```

### 5. Performance Monitoring

```bash
# Check response times
railway logs --filter "@path:/api" --lines 100 | grep "duration"

# Monitor specific endpoint
railway logs --filter "@path:/api/sessions/voice-demo" --lines 50

# Track error rates
railway logs --filter "@level:error" --lines 100 | grep -c "error"
```

---

## Quick Reference

### Most Useful Commands

```bash
# 1. Quick deployment check
railway deployment list --limit 5

# 2. Latest errors
railway logs --filter "@level:error" --lines 20

# 3. Bot spawn status
railway logs --filter "Bot.*spawned" --lines 10

# 4. Current deployment logs
railway logs --lines 100

# 5. Search for specific issue
railway logs --filter "your-keyword" --lines 50
```

### Common Filters

```
Text search:           "keyword"
Log level:             @level:error, @level:warn, @level:info
HTTP status:           @httpStatus:500, @httpStatus:200
HTTP method:           @method:POST, @method:GET
URL path:              @path:/api/endpoint
Service (env logs):    @service:<service_id>
Replica:               replica:<replica_id>

Operators:             AND, OR, - (NOT)
```

### Tips

- Use `--lines` to avoid streaming and get immediate results
- Use `--json` for automation and parsing
- Combine `--filter` with Unix tools for advanced queries
- Use deployment IDs to investigate specific deployments
- Stream logs without `--lines` to monitor in real-time

---

**Last Updated:** 2025-10-18
**Railway CLI Version:** Check with `railway --version`
