Logtail Rust
Logtail rust is an http wrapper for sending logs to betterstack
Environment Variables
| Variable | Description |
|---|---|
| ENVIRONMENT | Can be "local", "qa", "preprod" or "prod" |
| LOGS_SOURCE_TOKEN | Docs |
Usage
Basic usage
let logger = new;
let log = LogSchema ;
// Log methods now return Result<(), LogtailError>
logger.info.await?;
Builder pattern
use ;
use Duration;
let logger = builder
.app_version
.verbose
.max_retries
.base_delay
.max_delay
.jitter
.build;
logger.info.await?;
Custom retry config
use RetryConfig;
use Duration;
let retry = RetryConfig ;
let logger = with_retry;
Default (reads from env vars, default retry)
let logger = default;
Configuration Reference
Logger options
| Option | Type | Default | Description |
|---|---|---|---|
app_version |
String |
CARGO_PKG_VERSION |
Version string included in every log sent to BetterStack |
verbose |
bool |
true |
When true, logs are also printed to stdout (info/warn/debug) or stderr (error) in addition to being sent over HTTP |
environment |
EnvEnum |
Read from ENVIRONMENT env var |
One of Local, QA, PreProd, Prod. When set to Local, HTTP calls are skipped entirely |
logs_source_token |
String |
Read from LOGS_SOURCE_TOKEN env var |
BetterStack source token for authentication |
Retry options
| Option | Type | Default | Description |
|---|---|---|---|
max_retries |
u32 |
3 |
Maximum number of retry attempts after the initial call fails |
base_delay |
Duration |
1000ms |
Initial delay before the first retry, doubled on each subsequent attempt |
max_delay |
Duration |
5000ms |
Upper bound on the backoff delay |
jitter |
bool |
true |
Randomize the delay (0 to computed delay) to avoid thundering herd |
Retry Behavior
Failed HTTP requests are automatically retried with exponential backoff. Only transient errors are retried:
- 5xx HTTP errors — retried (server errors)
- Network errors — retried (connection failures, timeouts)
- 4xx HTTP errors — not retried (client errors)
- Serialization errors — not retried
Error Handling
All log methods (info, warn, error, debug) return Result<(), LogtailError>. Errors are no longer silently swallowed.
LogtailError variants:
Http { status, message }— HTTP error with status codeSerialization(serde_json::Error)— JSON serialization failureNetwork(reqwest::Error)— Network/connection error
Migration from 0.2.x
- Breaking:
info(),warn(),error(),debug()now returnResult<(), LogtailError>instead of() - Breaking:
push_log()now returnsResult<Option<Value>, LogtailError>instead ofOption<Value> - Add
.await?orlet _ =to log calls to handle the new return type tokiowithtimefeature is now a required dependency- New
Logger::builder(),Logger::with_retry()constructors available