-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(rust): add docs for logs #13945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
--- | ||
title: Set Up Logs in Rust | ||
sidebar_title: Logs | ||
description: "Structured logs allow you to send, view, and query logs sent from your applications within Sentry." | ||
sidebar_order: 5600 | ||
--- | ||
|
||
<Include name="feature-stage-beta-logs.mdx" /> | ||
|
||
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. | ||
|
||
## Requirements | ||
|
||
Logs in Rust are supported in Sentry Rust SDK version `0.39.0` and above. | ||
Additionally, the `logs` feature flag needs to be enabled. | ||
|
||
```toml {filename:Cargo.toml} | ||
[dependencies] | ||
sentry = { version = "{{@inject packages.version('sentry.rust') }}", features = ["logs"] } | ||
``` | ||
|
||
## Setup | ||
|
||
To enable logging, you need to initialize the SDK with the `enable_logs` option set to `true`. | ||
|
||
```rust | ||
let _guard = sentry::init(("___PUBLIC_DSN___", sentry::ClientOptions { | ||
release: sentry::release_name!(), | ||
enable_logs: true, | ||
..Default::default() | ||
})); | ||
``` | ||
|
||
## Usage | ||
|
||
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: | ||
|
||
```rust | ||
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: | ||
|
||
```rust | ||
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. | ||
|
||
## Integrations | ||
|
||
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](https://github.com/getsentry/sentry-rust/issues/new/choose) for any additional integration you would like to see. | ||
- [`tracing`](https://github.com/getsentry/sentry-rust/issues/799) | ||
- [`log`](https://github.com/getsentry/sentry-rust/issues/818) | ||
|
||
## Options | ||
|
||
### `before_send_log` | ||
|
||
To filter logs, or update them before they are sent to Sentry, you can use the `before_send_log` client option. | ||
|
||
```rust | ||
let _guard = sentry::init(("___PUBLIC_DSN___", 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() | ||
})); | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to a technical limitation in macros visibility in Rust, these need to be exported at top-level.
We named them with a
logger_
prefix to sort of follow the spec and prevent confusion with e.g.capture_error
.