Set Up Logs in Rust
Structured logs allow you to send, view, and query logs sent from your applications within Sentry.
With Sentry Structured Logs, you can send text based log information from your applications to Sentry. Once in Sentry, these logs can be viewed alongside relevant errors, searched by text-string, or searched using their individual attributes.
Logs in Rust are supported in Sentry Rust SDK version 0.39.0
and above. Additionally, the logs
feature flag needs to be enabled.
Cargo.toml
[dependencies]
sentry = { version = "0.39.0", features = ["logs"] }
To enable logging, you need to initialize the SDK with the enable_logs
option set to true
.
let _guard = sentry::init(("https://examplePublicKey@o0.ingest.sentry.io/0", sentry::ClientOptions {
release: sentry::release_name!(),
enable_logs: true,
..Default::default()
}));
Once the feature is enabled on the SDK and the SDK is initialized, you can send logs by using the logging macros. The sentry
crate exposes macros that support six different log levels: logger_trace
, logger_debug
, logger_info
, logger_warn
, logger_error
and logger_fatal
. The macros support logging a simple message, or a message with parameters, with format
syntax:
use sentry::logger_info;
logger_info!("Hello, world!");
logger_info!("Hello, {}!", "world");
You can also attach additional attributes to a log using the key = value
syntax before the message:
use sentry::logger_error;
logger_error!(
database.host = "prod-db-01",
database.port = 5432,
database.name = "user_service",
retry_attempt = 2,
beta_features = false,
"Database connection failed"
);
The supported attribute keys consist of any number of valid Rust identifiers, separated by dots. Attributes containing dots will be nested under their common prefix when displayed in the UI.
The supported attribute values correspond to the values that can be converted to a serde_json::Value
, which include primitive types for numbers, bool
, and string types. As of today, array and object types will be converted to strings using their JSON representation.
We're actively working on adding integration support for Logs. Currently, we're looking at adding support for the tracing
and log
crates. You can follow progress on the following GitHub issues or open a new one for any additional integration you would like to see.
To filter logs, or update them before they are sent to Sentry, you can use the before_send_log
client option.
let _guard = sentry::init(("https://examplePublicKey@o0.ingest.sentry.io/0", sentry::ClientOptions {
release: sentry::release_name!(),
enable_logs: true,
before_send_log: Some(std::sync::Arc::new(|log| {
// filter out all trace level logs
if log.level == sentry::protocol::LogLevel::Trace {
return None;
}
Some(log)
})),
..Default::default()
}));
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").