Skip to content

Commit 335ae8f

Browse files
authored
handle panics by default (#97)
1 parent cec6cdd commit 335ae8f

File tree

12 files changed

+106
-63
lines changed

12 files changed

+106
-63
lines changed

examples/actix-web.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ struct CreateUserRequest {
5050
async fn main() -> Result<(), Box<dyn std::error::Error>> {
5151
// Initialize Logfire
5252
let logfire = logfire::configure()
53-
.install_panic_handler()
5453
.finish()
5554
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
5655
let _guard = logfire.shutdown_guard();

examples/axum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct CreateUserRequest {
5959
#[tokio::main]
6060
async fn main() -> Result<(), Box<dyn std::error::Error>> {
6161
// Initialize Logfire
62-
let logfire = logfire::configure().install_panic_handler().finish()?;
62+
let logfire = logfire::configure().finish()?;
6363
let _guard = logfire.shutdown_guard();
6464

6565
logfire::info!("Starting Axum server with Logfire integration");

examples/basic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static FILES_COUNTER: LazyLock<Counter<u64>> = LazyLock::new(|| {
1515
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
1616

1717
fn main() -> Result<()> {
18-
let logfire = logfire::configure().install_panic_handler().finish()?;
18+
let logfire = logfire::configure().finish()?;
1919
let _guard = logfire.shutdown_guard();
2020

2121
let mut total_size = 0u64;

src/bridges/log.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ mod tests {
8181
let logfire = crate::configure()
8282
.local()
8383
.send_to_logfire(false)
84-
.install_panic_handler()
8584
.with_default_level_filter(TracingLevelFilter::TRACE)
8685
.with_advanced_options(
8786
AdvancedOptions::default()
@@ -179,7 +178,7 @@ mod tests {
179178
"code.lineno",
180179
),
181180
Int(
182-
26,
181+
25,
183182
),
184183
),
185184
),
@@ -312,7 +311,7 @@ mod tests {
312311
"code.lineno",
313312
),
314313
Int(
315-
27,
314+
26,
316315
),
317316
),
318317
),
@@ -445,7 +444,7 @@ mod tests {
445444
"code.lineno",
446445
),
447446
Int(
448-
30,
447+
29,
449448
),
450449
),
451450
),
@@ -578,7 +577,7 @@ mod tests {
578577
"code.lineno",
579578
),
580579
Int(
581-
31,
580+
30,
582581
),
583582
),
584583
),
@@ -711,7 +710,7 @@ mod tests {
711710
"code.lineno",
712711
),
713712
Int(
714-
32,
713+
31,
715714
),
716715
),
717716
),
@@ -844,7 +843,7 @@ mod tests {
844843
"code.lineno",
845844
),
846845
Int(
847-
33,
846+
32,
848847
),
849848
),
850849
),
@@ -977,7 +976,7 @@ mod tests {
977976
"code.lineno",
978977
),
979978
Int(
980-
34,
979+
33,
981980
),
982981
),
983982
),
@@ -1065,7 +1064,6 @@ mod tests {
10651064
.local()
10661065
.send_to_logfire(false)
10671066
.with_console(Some(console_options.clone()))
1068-
.install_panic_handler()
10691067
.with_default_level_filter(TracingLevelFilter::TRACE)
10701068
.finish()
10711069
.unwrap();

src/bridges/tracing.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,6 @@ mod tests {
535535
.with_additional_span_processor(SimpleSpanProcessor::new(
536536
DeterministicExporter::new(exporter.clone(), TEST_FILE, TEST_LINE),
537537
))
538-
.install_panic_handler()
539538
.with_default_level_filter(LevelFilter::TRACE)
540539
.with_advanced_options(
541540
AdvancedOptions::default()
@@ -613,7 +612,7 @@ mod tests {
613612
"code.lineno",
614613
),
615614
value: I64(
616-
28,
615+
27,
617616
),
618617
},
619618
KeyValue {
@@ -719,7 +718,7 @@ mod tests {
719718
"code.lineno",
720719
),
721720
value: I64(
722-
29,
721+
28,
723722
),
724723
},
725724
KeyValue {
@@ -835,7 +834,7 @@ mod tests {
835834
"code.lineno",
836835
),
837836
value: I64(
838-
29,
837+
28,
839838
),
840839
},
841840
KeyValue {
@@ -957,7 +956,7 @@ mod tests {
957956
"code.lineno",
958957
),
959958
value: I64(
960-
30,
959+
29,
961960
),
962961
},
963962
KeyValue {
@@ -1073,7 +1072,7 @@ mod tests {
10731072
"code.lineno",
10741073
),
10751074
value: I64(
1076-
30,
1075+
29,
10771076
),
10781077
},
10791078
KeyValue {
@@ -1195,7 +1194,7 @@ mod tests {
11951194
"code.lineno",
11961195
),
11971196
value: I64(
1198-
31,
1197+
30,
11991198
),
12001199
},
12011200
KeyValue {
@@ -1311,7 +1310,7 @@ mod tests {
13111310
"code.lineno",
13121311
),
13131312
value: I64(
1314-
31,
1313+
30,
13151314
),
13161315
},
13171316
KeyValue {
@@ -1433,7 +1432,7 @@ mod tests {
14331432
"code.lineno",
14341433
),
14351434
value: I64(
1436-
28,
1435+
27,
14371436
),
14381437
},
14391438
KeyValue {
@@ -1573,7 +1572,7 @@ mod tests {
15731572
"code.lineno",
15741573
),
15751574
Int(
1576-
25,
1575+
24,
15771576
),
15781577
),
15791578
),
@@ -1706,7 +1705,7 @@ mod tests {
17061705
"code.lineno",
17071706
),
17081707
Int(
1709-
26,
1708+
25,
17101709
),
17111710
),
17121711
),
@@ -1849,7 +1848,7 @@ mod tests {
18491848
"code.lineno",
18501849
),
18511850
Int(
1852-
33,
1851+
32,
18531852
),
18541853
),
18551854
),
@@ -1982,7 +1981,7 @@ mod tests {
19821981
"code.lineno",
19831982
),
19841983
Int(
1985-
34,
1984+
33,
19861985
),
19871986
),
19881987
),
@@ -2082,7 +2081,6 @@ mod tests {
20822081
.local()
20832082
.send_to_logfire(false)
20842083
.with_console(Some(console_options.clone()))
2085-
.install_panic_handler()
20862084
.with_default_level_filter(LevelFilter::TRACE)
20872085
.finish()
20882086
.unwrap();
@@ -2138,7 +2136,6 @@ mod tests {
21382136
.local()
21392137
.send_to_logfire(false)
21402138
.with_console(Some(console_options))
2141-
.install_panic_handler()
21422139
.with_default_level_filter(LevelFilter::INFO)
21432140
.finish()
21442141
.unwrap();
@@ -2278,7 +2275,6 @@ mod tests {
22782275
.with_metrics(Some(
22792276
crate::config::MetricsOptions::default().with_additional_reader(reader.clone()),
22802277
))
2281-
.install_panic_handler()
22822278
.with_default_level_filter(LevelFilter::TRACE)
22832279
.with_advanced_options(
22842280
AdvancedOptions::default()

src/config.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use tracing::{Level, level_filters::LevelFilter};
2020
use crate::{ConfigureError, logfire::Logfire};
2121

2222
/// Builder for logfire configuration, returned from [`logfire::configure()`][crate::configure].
23-
#[derive(Default)]
2423
#[must_use = "call `.finish()` to complete logfire configuration."]
2524
pub struct LogfireConfigBuilder {
2625
pub(crate) local: bool,
@@ -52,6 +51,23 @@ pub struct LogfireConfigBuilder {
5251
pub(crate) default_level_filter: Option<LevelFilter>,
5352
}
5453

54+
impl Default for LogfireConfigBuilder {
55+
fn default() -> Self {
56+
Self {
57+
local: false,
58+
send_to_logfire: None,
59+
token: None,
60+
console_options: None,
61+
data_dir: None,
62+
additional_span_processors: Vec::new(),
63+
advanced: None,
64+
metrics: None,
65+
install_panic_handler: true,
66+
default_level_filter: None,
67+
}
68+
}
69+
}
70+
5571
impl LogfireConfigBuilder {
5672
/// Call to configure Logfire for local use only.
5773
///
@@ -62,11 +78,17 @@ impl LogfireConfigBuilder {
6278
self
6379
}
6480

65-
/// Call to install a hook to log panics.
81+
/// Whether to install a hook to
6682
///
6783
/// Any existing panic hook will be preserved and called after the logfire panic hook.
68-
pub fn install_panic_handler(mut self) -> Self {
69-
self.install_panic_handler = true;
84+
pub fn with_install_panic_handler(mut self, install: bool) -> Self {
85+
self.install_panic_handler = install;
86+
self
87+
}
88+
89+
/// Deprecated form of [`with_install_panic_handler`][Self::with_install_panic_handler].
90+
#[deprecated(since = "0.8.0", note = "noop; now installed by default")]
91+
pub fn install_panic_handler(self) -> Self {
7092
self
7193
}
7294

src/internal/exporters/console.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,6 @@ mod tests {
410410
.local()
411411
.send_to_logfire(false)
412412
.with_console(Some(console_options))
413-
.install_panic_handler()
414413
.with_default_level_filter(LevelFilter::TRACE)
415414
.finish()
416415
.unwrap();
@@ -457,7 +456,6 @@ mod tests {
457456
.local()
458457
.send_to_logfire(false)
459458
.with_console(Some(console_options))
460-
.install_panic_handler()
461459
.with_default_level_filter(LevelFilter::TRACE)
462460
.finish()
463461
.unwrap();
@@ -503,7 +501,6 @@ mod tests {
503501
.local()
504502
.send_to_logfire(false)
505503
.with_console(Some(console_options))
506-
.install_panic_handler()
507504
.with_default_level_filter(LevelFilter::TRACE)
508505
.finish()
509506
.unwrap();
@@ -541,7 +538,6 @@ mod tests {
541538
let shutdown_handler = crate::configure()
542539
.send_to_logfire(false)
543540
.local()
544-
.install_panic_handler()
545541
.finish()
546542
.unwrap();
547543

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
//! ```rust
4747
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
4848
//! let logfire = logfire::configure()
49-
//! .install_panic_handler()
5049
//! # .send_to_logfire(logfire::config::SendToLogfire::IfTokenPresent)
5150
//! .finish()?;
5251
//!
@@ -96,7 +95,7 @@ pub use macros::*;
9695
pub use metrics::*;
9796

9897
pub use crate::bridges::tracing::LogfireTracingLayer;
99-
pub use crate::logfire::Logfire;
98+
pub use crate::logfire::{Logfire, ShutdownGuard};
10099

101100
mod internal;
102101

@@ -186,7 +185,6 @@ pub enum ShutdownError {
186185
/// ```rust
187186
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
188187
/// let logfire = logfire::configure()
189-
/// .install_panic_handler()
190188
/// # .send_to_logfire(logfire::config::SendToLogfire::IfTokenPresent)
191189
/// .finish()?;
192190
///

src/logfire.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ impl Logfire {
7373
/// ```rust
7474
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
7575
/// let logfire = logfire::configure()
76-
/// .install_panic_handler()
7776
/// # .send_to_logfire(logfire::config::SendToLogfire::IfTokenPresent)
7877
/// .finish()?;
7978
///
@@ -1062,4 +1061,36 @@ mod tests {
10621061
}
10631062
// temp_dir is cleaned up automatically
10641063
}
1064+
1065+
#[test]
1066+
fn test_panic_handler_disabled() {
1067+
// other tests check enabled (default)
1068+
1069+
use crate::config::AdvancedOptions;
1070+
use opentelemetry_sdk::logs::{InMemoryLogExporter, SimpleLogProcessor};
1071+
1072+
let log_exporter = InMemoryLogExporter::default();
1073+
1074+
let logfire = configure()
1075+
.local()
1076+
.send_to_logfire(false)
1077+
.with_install_panic_handler(false)
1078+
.with_advanced_options(
1079+
AdvancedOptions::default()
1080+
.with_log_processor(SimpleLogProcessor::new(log_exporter.clone())),
1081+
)
1082+
.finish()
1083+
.expect("failed to configure logfire");
1084+
1085+
let guard = crate::set_local_logfire(logfire);
1086+
1087+
let _result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
1088+
panic!("test panic");
1089+
}));
1090+
1091+
guard.shutdown().expect("shutdown should succeed");
1092+
1093+
let logs = log_exporter.get_emitted_logs().unwrap();
1094+
assert!(logs.is_empty());
1095+
}
10651096
}

0 commit comments

Comments
 (0)