
# Logging

Queuety logs every job and workflow event to the `queuety_logs` database table. There are no log files. All entries are queryable via the PHP API and CLI.

## Event types

Every log entry has an event type from the `LogEvent` enum:

| Event | Description |
|---|---|
| `started` | Job execution started |
| `completed` | Job completed successfully |
| `failed` | An attempt threw and will be retried (the final attempt logs only `buried`) |
| `buried` | Job exhausted all retry attempts |
| `retried` | Job was scheduled for retry |
| `workflow_started` | Workflow was dispatched |
| `workflow_completed` | All workflow steps finished |
| `workflow_failed` | Workflow step failed permanently |
| `workflow_paused` | Workflow was paused |
| `workflow_resumed` | Workflow was resumed |
| `workflow_cancelled` | Workflow was cancelled |
| `workflow_rewound` | Workflow was rewound to an earlier step |
| `workflow_forked` | Workflow was forked into a new workflow |
| `workflow_deadline_exceeded` | Workflow missed its `must_complete_within()` deadline |
| `debug` | Debug-level message (when `QUEUETY_DEBUG` is enabled) |

## Log fields

Each log entry contains:

| Field | Description |
|---|---|
| `id` | Auto-increment ID |
| `event` | Event type string |
| `handler` | Handler name or class |
| `job_id` | Associated job ID (nullable) |
| `workflow_id` | Associated workflow ID (nullable) |
| `step_index` | Workflow step index (nullable) |
| `attempt` | Attempt number |
| `duration_ms` | Execution duration in milliseconds |
| `error_message` | Error message if failed |
| `created_at` | Timestamp |

## Querying logs via PHP

```php
$logger = Queuety::logger();

// Logs for a specific job
$logs = $logger->for_job( 42 );

// Logs for a specific workflow
$logs = $logger->for_workflow( 7 );

// Logs for a specific handler
$logs = $logger->for_handler( 'send_email', 50 );

// Logs for a specific event type
$logs = $logger->for_event( \Queuety\Enums\LogEvent::Failed, 50 );

// Logs since a datetime
$logs = $logger->since( new \DateTimeImmutable( '-1 hour' ), 100 );
```

## Querying logs via CLI

```bash
# Recent logs (last 24 hours)
wp queuety log

# Logs for a specific job
wp queuety log --job=42

# Logs for a specific workflow
wp queuety log --workflow=7

# Logs for a handler
wp queuety log --handler=send_email

# Logs for an event type
wp queuety log --event=failed

# Logs since a datetime
wp queuety log --since="2025-01-01 00:00:00"

# Custom limit and format
wp queuety log --limit=100 --format=json
```

## Log retention

Worker maintenance purges completed jobs and old logs after their configured retention periods. `QUEUETY_LOG_RETENTION_DAYS = 0` keeps logs indefinitely; setting either retention period to zero disables that automatic purge. Completed jobs required by unfinished dependency chains are retained.

Purge from PHP, for example from your own scheduled task or with `wp eval`:

```php
$count = Queuety::logger()->purge( 30 );
```
