Introduction
Flare can collect the log lines of your Rust app and show them in the logs view of your project, next to your errors and traces. The same lines also show up as breadcrumbs on your error reports, and inside the traces they belong to.
Rust has two logging crates, and Flare plugs into both:
tracingis what most modern apps use, and what tokio, axum, Actix Web and SQLx are built on. Its events are log lines with typed fields, and its spans are what traces are made of.logis the older and simpler one. Small tools often use it together withenv_logger.
Most apps end up with both, because every dependency picks its own. If your app uses tracing, use the tracing integration: tracing-subscriber turns the log lines of your dependencies into tracing events, so you get those too, and it's the only way to send traces. Only reach for the log integration when your app doesn't use tracing at all.
Both integrations can do three things:
- Breadcrumbs, on by default. Log lines at INFO or above show up on the timeline of the next error report.
- Logs, off by default.
send_logs(level)sends your log lines to Flare. - Error reports, off by default.
report_errors(true)also reports everyerror!line as an error.
Logs and error reports count against your Flare quota, which is why you turn those on yourself.
With tracing
Turn on the tracing feature:
Terminal
cargo add flareapp --features tracing
cargo add tracing tracing-subscriber
Then add the Flare layer to your subscriber, next to the layer that prints your logs:
src/main.rs
use tracing_subscriber::{filter::LevelFilter, prelude::*};
#[flareapp::main]
fn main() {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().with_filter(LevelFilter::INFO))
.with(flareapp::tracing::layer().send_logs(tracing::Level::INFO))
.init();
tracing::info!(order_id = 7, total = 49.95, "order placed");
}
send_logs(tracing::Level::INFO) sends events at INFO, WARN and ERROR. Pass tracing::Level::WARN if you only want the warnings and errors.
The layer needs a subscriber built on tracing_subscriber::registry(). The tracing_subscriber::fmt() builder uses one too, so that works as well.
Where your filters go
A filter on the whole subscriber, like .with(LevelFilter::WARN) or an EnvFilter built from RUST_LOG, decides which events reach any layer at all. Anything it drops never gets to Flare, whatever level you gave send_logs. That includes the DEBUG events that SQLx writes for its queries. Put the filter on the fmt layer instead, so it only decides what your app prints:
Rust
use tracing_subscriber::{filter::LevelFilter, prelude::*};
// Flare only gets WARN and ERROR, and no SQLx queries.
tracing_subscriber::registry()
.with(LevelFilter::WARN)
.with(tracing_subscriber::fmt::layer())
.with(flareapp::tracing::layer().send_logs(tracing::Level::INFO))
.init();
// Your app prints WARN and ERROR, and Flare gets everything it asks for.
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().with_filter(LevelFilter::WARN))
.with(flareapp::tracing::layer().send_logs(tracing::Level::INFO))
.init();
With log
Turn on the log feature:
Terminal
cargo add flareapp --features log
cargo add log env_logger
The log crate allows only one global logger. So instead of adding Flare next to your logger, Flare's logger wraps the one you already use and passes every record on to it:
src/main.rs
#[flareapp::main]
fn main() -> Result<(), Box<dyn std::error::Error>> {
flareapp::log::Logger::new()
.forward_to(env_logger::Builder::from_default_env().build())
.send_logs(log::Level::Info)
.install()?;
log::info!("import started");
Ok(())
}
Your own logger still decides what gets printed. Without forward_to, Flare's logger only collects what it sends to Flare, and nothing is printed. install() fails when another global logger is already in place.
Breadcrumbs, send_logs and report_errors work the same way on the log logger. The methods take a log::Level instead of a tracing::Level. The log logger takes no part in traces: its lines carry no trace id or span id, and they don't show up as log rows inside a trace.
What a log line contains
Flare shows the message and the level of each log line. Everything else becomes an attribute you can search and filter on:
- The fields of a
tracingevent, likeorder_idandtotalin the example above. - The entry point: the request, job or command the line belongs to, like
GET /users/{id}. Flare shows it next to the line in the log list. - The target: the module that wrote the line, like
my_app::billing, unless you give the macro anothertarget:. Flare shows it in the details of the line, and you can filter on it astracing.targetwith thetracingintegration, orlog.targetwith thelogintegration. - The file and line in your code, and the name of the thread.
- The trace id and span id, when the line was written inside a traced span. They show up in the details of the log line, so you can jump to the trace it belongs to.
On top of that, every line carries the same application info as your error reports: the name and version of your crate, the environment, the host and the Rust version.
Breadcrumbs
Both integrations keep your recent log lines in the current scope. When an error is reported, those lines show up on its timeline, next to your glows. It's on by default for lines at INFO and above, and it doesn't send a single log line to Flare.
A scope keeps the last 100 lines. With the axum or Actix Web middleware, every request has its own scope, so an error never shows the lines of another request. Lines written outside a request or job, like at startup, go to the app-wide scope, and those show up on every error.
To change the level, or turn breadcrumbs off:
Rust
flareapp::tracing::layer()
.breadcrumbs(Some(tracing::Level::DEBUG));
flareapp::tracing::layer()
.breadcrumbs(None);
Reporting error! lines as errors
Plenty of code logs a failure and moves on. If you want those failures in your error list as well, turn on report_errors:
Rust
flareapp::tracing::layer()
.send_logs(tracing::Level::INFO)
.report_errors(true);
From then on, every line at the ERROR level is also reported, like a message:
Rust
tracing::error!(order_id = 7, provider = "mollie", "payment failed");
The report gets the exception class Log and the message of the line, and the fields of the line show up as context under log. Its stack trace points at the line in your code that logged it. Flare groups these reports on their level and message, not on that place. Ids, numbers, dates and email addresses in the message are left out first, so payment 7 failed and payment 8 failed end up as one error in Flare.
When the line carries the error itself, in a field named error, the report shows that error instead. Flare takes the exception class from the name at the start of its Debug output, like RowNotFound, and the message from its text. The message of the line goes into the log context, and the causes of the error under error:
Rust
if let Err(error) = charge(order) {
tracing::error!(error = &error as &dyn std::error::Error, "payment failed");
}
Logs inside a trace
With tracing turned on, a tracing event at INFO or above that happens inside a traced span is also added to that span. The trace then shows it as a log row, right where it happened. This doesn't depend on send_logs.
An event inside a span that Flare doesn't send, like a DEBUG span, moves up to the closest parent span that is sent. See which spans are sent.
An error! event inside a span also marks that span as failed. To change the level of the log rows, or turn them off:
Rust
flareapp::tracing::layer()
.span_events(Some(tracing::Level::WARN));
How logs are sent
Log lines are collected on a background thread and sent in batches, so your app never waits on the network. A batch goes out when it holds 100 lines, or when its oldest line has waited 5 seconds, whichever comes first. When your program ends, #[flareapp::main] or the guard of flareapp::init sends what's left.
Log lines, traces and error reports wait in one queue, which holds up to 1024 records. When it's full, Flare drops new ones instead of making your app wait, so a burst of logs never slows you down. While lines wait for their batch, Flare keeps at most 1024 of them, and drops the oldest first. A request that fails without an answer, like a connection error or a timeout, or with a 429, 502, 503 or 504 response, is tried again up to 3 times.
You can change those numbers on the config:
Rust
use std::time::Duration;
fn flare_config() -> flareapp::Config {
flareapp::config!()
.batch_size(200)
.batch_interval(Duration::from_secs(10))
.queue_capacity(4096)
}
#[flareapp::main(config = flare_config)]
fn main() {}
Logs need the private API key of your project in FLARE_KEY. The public key only works for errors.
Read more
- Working with logs: searching and filtering your logs in Flare.
- Adding glows: breadcrumbs you add yourself.
- Logs payload in the protocol docs.