一键导入
rust-logging
Use when setting up tracing, configuring log levels, writing structured logs, using spans and instrument, or handling sensitive data in Rust logs.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use when setting up tracing, configuring log levels, writing structured logs, using spans and instrument, or handling sensitive data in Rust logs.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Use when designing REST API endpoints, URL structure, HTTP methods, status codes, request/response formats, pagination, SSE streaming, or OpenAPI docs in Axum.
Use when creating modules, organizing project structure, designing layered architecture (handler/service/domain/repo), dependency injection, or DDD patterns in Rust.
Use when designing PostgreSQL schemas, writing SQLx migrations, choosing data types, naming tables/columns/indexes, or writing compile-time checked queries.
Use when implementing error types, designing error hierarchies, mapping errors to HTTP responses, or reviewing error handling patterns in Rust code using thiserror and anyhow.
Use when writing unit/integration tests, setting up test infrastructure, mocking with wiremock/mockall, or designing test structure in Rust.
| name | rust-logging |
| description | Use when setting up tracing, configuring log levels, writing structured logs, using spans and instrument, or handling sensitive data in Rust logs. |
tracing + tracing-subscriber| 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
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
#[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> {
// ...
}
skip large or non-Display arguments (self, request bodies, DB pools).fields(...) to attach key context to the span.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: 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 messagetracing macros, never println! or logrequest_id for distributed tracing