Rust logging
Use when setting up tracing, configuring log levels, writing structured logs, using spans and instrument, or handling sensitive data in Rust logs.From its SKILL.md
npx -y skills add longzhi/rust-coding-standards --skill rust-loggingAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 2 stars2 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
2.7 KB, as published. Nobody here has run it
Rust Logging Standards
Core Rules
- Framework:
tracing+tracing-subscriber - Format: pretty print in dev, JSON in production
- Output: stdout
- Language: all log messages in English
Log Levels
| Level | Purpose | Example |
|---|---|---|
ERROR | Requires alerting and human intervention | Database connection failed |
WARN | Needs attention but system can self-heal | Retry succeeded after transient failure |
INFO | Business events, visible in production | Order created/completed |
DEBUG | Development debugging, disabled in prod | SQL queries, intermediate values |
TRACE | Extreme detail, only for hard-to-diagnose issues | Function entry/exit |
# Production
RUST_LOG=info,my_app=debug
# Development
RUST_LOG=debug,my_app=trace,sqlx=warn
Structured Logging
Use snake_case field names:
tracing::info!(
request_id = %request_id,
user_id = %user_id,
order_id = %order.id,
duration_ms = elapsed.as_millis() as u64,
"order completed"
);
Common fields: request_id, user_id, order_id, duration_ms, error, path, method, status
Span Usage
#[tracing::instrument(
name = "create_order",
skip(self, cmd),
fields(user_id = %cmd.user_id, product_id = %cmd.product_id)
)]
pub async fn create_order(&self, cmd: CreateOrderCommand) -> Result<Order, AppError> {
// ...
}
- Always
skiplarge or non-Display arguments (self, request bodies, DB pools). - Use
fields(...)to attach key context to the span.
Sensitive Data
Never log: passwords, API keys, tokens, credit card numbers, personal IDs.
// Use the secrecy crate
use secrecy::Secret;
pub struct Config {
pub api_key: Secret<String>, // Displays as [REDACTED] in logs
}
Error Logging
// ERROR: requires alerting
tracing::error!(error = ?err, "database connection failed");
// WARN: needs attention
tracing::warn!(error = ?err, attempt = attempt, "retry after failure");
// INFO: expected business error
tracing::info!(error = %err, "validation failed");
?err— preserves the full error chain (Debug format)%err— logs only the Display message
Checklist
- Use
tracingmacros, neverprintln!orlog - All log messages in English
- Key business operations logged at INFO level
- Errors use the correct severity level
- Include
request_idfor distributed tracing - Sensitive data is redacted
- Use structured fields instead of string concatenation