1
0
mirror of https://github.com/actix/actix-website synced 2024-12-18 09:53:12 +01:00
actix-website/content/docs/sentry.cn.md

61 lines
1.6 KiB
Markdown
Raw Normal View History

2018-06-22 17:18:14 +02:00
---
title: Sentry
menu: docs_patterns
weight: 1020
---
# Sentry崩溃报告
[Sentry](https://sentry.io/)是一个崩溃报告系统它支持基于actix错误报告的failure crate。使用Sentry中间件可以自动将服务器错误报告给Sentry。
# Sentry中间件
该中间件捕获服务器错误范围500-599中的任何错误并通过其堆栈跟踪将附加的错误发送给哨兵。
要使用中间件需要初始化和配置Sentry并且需要添加sentry-actix中间件。此外注册panic处理程序以通知困难panic也是有意义的。
```rust
extern crate sentry;
extern crate sentry_actix;
use sentry_actix::SentryMiddleware;
use std::env;
fn main() {
sentry::init("SENTRY_DSN_GOES_HERE");
env::set_var("RUST_BACKTRACE", "1");
sentry::integrations::panic::register_panic_handler();
let mut app = App::with_state(state)
.middleware(SentryMiddleware::new())
// ...
}
```
# Reusing the Hub
如果使用这种集成默认的sentry hubHub::current()通常是错误的。要获得特定的请求您需要使用ActixWebHubExt trait
```rust
use sentry::{Hub, Level};
use sentry_actix::ActixWebHubExt;
let hub = Hub::from_request(req);
hub.capture_message("Something is not well", Level::Warning);
```
The hub can also be made current for the duration of a call. Then Hub::current() works correctly until the end of the run block.
```rust
use sentry::{Hub, Level};
use sentry_actix::ActixWebHubExt;
let hub = Hub::from_request(req);
Hub::run(hub, || {
sentry::capture_message("Something is not well", Level::Warning);
});
```