# Debugbar for Laravel Rust

A debug bar for Laravel for Rust applications. It shows each request, console command and queued job with its queries, log messages, cache, Redis, mail, view and HTTP client activity, exceptions, timeline, headers, cookies, session and Inertia props.
`FEATURES.md` compares it, feature by feature, with Laravel Debugbar for PHP.
## Installation
Add the crate:
```toml
[dependencies]
laravel-rust-debugbar = "0.1"
```
The crate uses PostgreSQL by default. For another database, turn off the default features and choose the one your app uses: `mysql` (also for MariaDB) or `sqlite`.
```toml
laravel-rust-debugbar = { version = "0.1", default-features = false, features = ["mysql"] }
```
The optional `sqlite-storage` and `redis-storage` features add the SQLite and Redis storages.
Register the provider in `src/app.rs`:
```rust
use laravel_rust_debugbar::DebugbarServiceProvider;
Application::configure()
.with_providers(|providers| {
providers.add(DebugbarServiceProvider);
})
```
The bar shows when `APP_DEBUG=true` and `APP_ENV` is not `production` or `testing`. Its history can only be read from local and private addresses unless you change `DEBUGBAR_OPEN_STORAGE`. Never turn it on in production.
## Settings
### General
| `DEBUGBAR_ENABLED` | unset | Turns the bar on or off, whatever the environment. |
| `DEBUGBAR_FORCE_ALLOW_ENABLE` | `false` | Sets the bar up but leaves it off, so a request can turn it on with `enable()`. |
| `DEBUGBAR_INJECT` | `true` | Adds the bar to HTML pages. When `false`, requests are still recorded. |
| `DEBUGBAR_ROUTE_PREFIX` | `_debugbar` | The path prefix of the bar's endpoints. |
| `DEBUGBAR_ROUTE_DOMAIN` | unset | Serves the bar's endpoints on this domain only. |
| `DEBUGBAR_EXCEPT` | empty | Paths that are never recorded, separated by commas. `*` matches anything, as in `/horizon*`. |
| `DEBUGBAR_EDITOR` | `vscode` | The editor that file links open: `vscode`, `cursor`, `phpstorm`, `zed`, `sublime` or `none`. Each browser can change it in the bar's settings. |
| `DEBUGBAR_REMOTE_SITES_PATH`, `DEBUGBAR_LOCAL_SITES_PATH` | unset | Maps the application's path in a container or VM to the path on your machine, for editor links. |
| `DEBUGBAR_SERVER_TIMING` | `false` | Adds a `Server-Timing` header with the request and database time. |
| `DEBUGBAR_CLOCKWORK` | `false` | Adds Clockwork headers and serves `/__clockwork/{id}` for the Clockwork browser extension. |
### What is collected
| `DEBUGBAR_DISABLED_COLLECTORS` | empty | Collectors to leave out, separated by commas: `queries`, `messages`, `timeline`, `headers`, `cookies`, `session`, `user`, `body`, `environment`, `memory`, `route`, `inertia`, `cache`, `redis`, `mail`, `views`, `http`, `exceptions`. |
| `DEBUGBAR_COLLECT_HEADERS` | `true` | Records request and response headers. |
| `DEBUGBAR_COLLECT_BODY` | `true` | Records JSON and form request bodies up to 64 KB. |
| `DEBUGBAR_COLLECT_INERTIA_PROPS` | `true` | Records the props of Inertia pages. |
| `DEBUGBAR_COLLECT_SESSION` | `false` | Records the session data. |
| `DEBUGBAR_COLLECT_USER` | `false` | Records the signed-in user's ID, or that the visitor is a guest. |
| `DEBUGBAR_COLLECT_CONFIG` | `false` | Lets the bar show the configuration. |
| `DEBUGBAR_LOG_TAIL` | `false` | Lets the bar show the end of the newest log file. |
| `DEBUGBAR_LOG_TAIL_LINES` | `124` | How many log lines it shows. |
| `DEBUGBAR_MASKED` | empty | More header, cookie, body and session names to mask, separated by commas. Passwords, tokens, cookies and CSRF headers are always masked. |
| `DEBUGBAR_VIEW_EXCLUDE` | empty | View names to leave out, by prefix. |
### Queries
| `DEBUGBAR_SLOW_QUERY_MS` | `100` | Queries at or above this time are marked slow. |
| `DEBUGBAR_ONLY_SLOW_QUERIES` | `false` | Keeps only the slow queries. |
| `DEBUGBAR_BACKTRACE` | `false` | Finds the application lines that ran each query. This adds time to each query. |
| `DEBUGBAR_BACKTRACE_FRAMES` | `5` | How many application lines it keeps per query. |
| `DEBUGBAR_BACKTRACE_EXCLUDE` | empty | Paths to skip in backtraces, by prefix. |
| `DEBUGBAR_QUERY_EXCLUDE_PATHS` | empty | Queries from these paths are counted but not listed. |
| `DEBUGBAR_QUERY_SOFT_LIMIT` | `100` | After this many queries, later ones have no bindings or backtrace. |
| `DEBUGBAR_QUERY_HARD_LIMIT` | `500` | After this many queries, later ones are only counted. |
| `DEBUGBAR_EXPLAIN` | `true` | Lets the bar run `EXPLAIN` on a `SELECT`, on PostgreSQL, MySQL, MariaDB and SQLite. On MySQL and MariaDB, a value that was not recorded is replaced by `1`, and the plan says so. |
| `DEBUGBAR_TIMELINE_QUERIES` | `false` | Shows each query as a bar in the timeline. |
| `DEBUGBAR_SHOW_QUERY_RESULT` | `false` | Lets the bar run a `SELECT` without bindings again and show its first 100 rows. |
### Storage
| `DEBUGBAR_STORAGE` | `file` | Where records are kept: `file`, `memory`, `sqlite` with the `sqlite-storage` feature, or `redis` with the `redis-storage` feature. File, SQLite and Redis storage keep records across restarts and let commands and queue workers add theirs. |
| `DEBUGBAR_REDIS_CONNECTION` | `default` | The app's Redis connection that Redis storage uses. Keys start with `debugbar:`. |
| `DEBUGBAR_STORAGE_PATH` | `storage/debugbar` | The folder for file storage, or the file for SQLite storage (`storage/debugbar.sqlite`). |
| `DEBUGBAR_HISTORY` | `50` | How many records are kept. Older ones are deleted. |
| `DEBUGBAR_HISTORY_HOURS` | `24` | Records older than this are deleted. `0` keeps them until the count limit removes them. |
| `DEBUGBAR_OPEN_STORAGE` | unset | Who may read the history. Unset allows only local and private addresses, `true` allows everyone, and `false` turns the history list off. |
To keep records somewhere else, implement `Storage` and call `laravel_rust_debugbar::use_storage(storage)` before the application starts.
## Protecting the bar's endpoints
To add middleware to the bar's endpoints, such as an authentication check, call this before the application starts:
```rust
laravel_rust_debugbar::use_route_middleware(RequireAdmin);
```
## Commands and jobs
The bar also records console commands and queued jobs, with their queries, log lines and timeline. They show in the request list next to the HTTP requests. Long-running commands such as `serve` and `queue:work` are not recorded, but each job the worker runs is.
## Console
```shell
laravel debugbar:find --uri "/users/*" --kind request
laravel debugbar:find --issues
laravel debugbar:get latest
laravel debugbar:queries latest --explain
laravel debugbar:clear
```
Each command takes `--json` for machine-readable output, except `debugbar:clear`.
## In your code
Every `tracing` line written during a request shows in the Messages tab with its file and line. The package also has these helpers:
```rust
use laravel_rust_debugbar as debugbar;
debugbar::dump(&order);
debugbar::message("billing", "Invoice sent");
debugbar::add_error(&error);
let report = debugbar::measure("monthly report", build_report()).await;
let rows = debugbar::measure_with("import", [("file", "users.csv")], import()).await;
debugbar::start_measure("render");
debugbar::stop_measure("render");
debugbar::collect("payments", &payment);
debugbar::disable();
```
A custom collector adds its own tab to every record:
```rust
struct SlowRequests;
impl debugbar::Collector for SlowRequests {
fn name(&self) -> &str {
"slow requests"
}
fn collect(&self, record: &debugbar::RequestRecord) -> Option<serde_json::Value> {
(record.duration_ms > 500.0).then(|| serde_json::json!({ "ms": record.duration_ms }))
}
}
debugbar::add_collector(SlowRequests);
```
## Limits
- Query values show as their type, such as `<Option<&str>>`, unless the query binds them with `bind_visible`.
- Memory is the process's peak, not the request's own use.
- Some features need data that Laravel for Rust does not expose yet, such as cache values, mail bodies, gate checks and dispatched events. The [issues](https://github.com/eznix86/laravel-rust-debugbar/issues) list them, with the framework change each one needs.